api-example.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 json
  9. import requests
  10. # Server address
  11. server = "127.0.0.1"
  12. # Generation parameters
  13. # Reference: https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.GenerationConfig
  14. params = {
  15. 'max_new_tokens': 200,
  16. 'do_sample': True,
  17. 'temperature': 0.5,
  18. 'top_p': 0.9,
  19. 'typical_p': 1,
  20. 'repetition_penalty': 1.05,
  21. 'encoder_repetition_penalty': 1.0,
  22. 'top_k': 0,
  23. 'min_length': 0,
  24. 'no_repeat_ngram_size': 0,
  25. 'num_beams': 1,
  26. 'penalty_alpha': 0,
  27. 'length_penalty': 1,
  28. 'early_stopping': False,
  29. 'seed': -1,
  30. }
  31. # Input prompt
  32. prompt = "What I would like to say is the following: "
  33. payload = json.dumps([prompt, params])
  34. response = requests.post(f"http://{server}:7860/run/textgen", json={
  35. "data": [
  36. payload
  37. ]
  38. }).json()
  39. reply = response["data"][0]
  40. print(reply)