api-example.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 'truncation_length': 2048,
  33. 'ban_eos_token': False,
  34. }
  35. # Input prompt
  36. prompt = "What I would like to say is the following: "
  37. payload = json.dumps([prompt, params])
  38. response = requests.post(f"http://{server}:7860/run/textgen", json={
  39. "data": [
  40. payload
  41. ]
  42. }).json()
  43. reply = response["data"][0]
  44. print(reply)