api-example.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.72,
  18. 'top_p': 0.73,
  19. 'typical_p': 1,
  20. 'repetition_penalty': 1.1,
  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. 'add_bos_token': True,
  31. 'custom_stopping_strings': [],
  32. }
  33. # Input prompt
  34. prompt = "What I would like to say is the following: "
  35. payload = json.dumps([prompt, params])
  36. response = requests.post(f"http://{server}:7860/run/textgen", json={
  37. "data": [
  38. payload
  39. ]
  40. }).json()
  41. reply = response["data"][0]
  42. print(reply)