api-example.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. '''
  2. This is an example on how to use the API for oobabooga/text-generation-webui.
  3. Make sure to start the web UI with the following flags:
  4. python server.py --model MODEL --listen --no-stream
  5. Optionally, you can also add the --share flag to generate a public gradio URL,
  6. allowing you to use the API remotely.
  7. '''
  8. import requests
  9. # Server address
  10. server = "127.0.0.1"
  11. # Generation parameters
  12. # Reference: https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.GenerationConfig
  13. params = {
  14. 'max_new_tokens': 200,
  15. 'do_sample': True,
  16. 'temperature': 0.5,
  17. 'top_p': 0.9,
  18. 'typical_p': 1,
  19. 'repetition_penalty': 1.05,
  20. 'encoder_repetition_penalty': 1.0,
  21. 'top_k': 0,
  22. 'min_length': 0,
  23. 'no_repeat_ngram_size': 0,
  24. 'num_beams': 1,
  25. 'penalty_alpha': 0,
  26. 'length_penalty': 1,
  27. 'early_stopping': False,
  28. }
  29. # Input prompt
  30. prompt = "What I would like to say is the following: "
  31. response = requests.post(f"http://{server}:7860/run/textgen", json={
  32. "data": [
  33. prompt,
  34. params['max_new_tokens'],
  35. params['do_sample'],
  36. params['temperature'],
  37. params['top_p'],
  38. params['typical_p'],
  39. params['repetition_penalty'],
  40. params['encoder_repetition_penalty'],
  41. params['top_k'],
  42. params['min_length'],
  43. params['no_repeat_ngram_size'],
  44. params['num_beams'],
  45. params['penalty_alpha'],
  46. params['length_penalty'],
  47. params['early_stopping'],
  48. ]
  49. }).json()
  50. reply = response["data"][0]
  51. print(reply)