TA commited on
Commit
1924ec9
·
verified ·
1 Parent(s): 3907c28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -5,6 +5,7 @@ import requests
5
  SYSTEM_PROMPT = "As an LLM, your job is to generate detailed prompts that start with generate the image, for image generation models based on user input. Be descriptive and specific, but also make sure your prompts are clear and concise."
6
  TITLE = "Image Prompter"
7
  EXAMPLE_INPUT = "A Man Riding A Horse in Space"
 
8
 
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
@@ -13,38 +14,38 @@ def build_input_prompt(message, chatbot, system_prompt):
13
  """
14
  Constructs the input prompt string from the chatbot interactions and the current message.
15
  """
16
- input_prompt = " \n" + system_prompt + " \n \n"
17
  for interaction in chatbot:
18
- input_prompt = input_prompt + str(interaction[0]) + " \n \n" + str(interaction[1]) + "\n \n \n"
19
 
20
- input_prompt = input_prompt + str(message) + " \n "
21
  return input_prompt
22
 
23
 
24
- def post_request(model_url, payload):
25
  """
26
- Sends a POST request to the specified model URL and returns the JSON response.
27
  """
28
- response = requests.post(model_url, headers=HEADERS, json=payload)
29
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
30
  return response.json()
31
 
32
 
33
- def predict(model_url, message, chatbot=[], system_prompt=""):
34
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
35
  data = {
36
  "inputs": input_prompt
37
  }
38
 
39
  try:
40
- response_data = post_request(model_url, data)
41
  json_obj = response_data[0]
42
 
43
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
44
  bot_message = json_obj['generated_text']
45
  return bot_message
46
  elif 'error' in json_obj:
47
- raise gr.Error(json_obj['error'] +'Please refresh and try again with smaller input prompt')
48
  else:
49
  warning_msg = f"Unexpected response: {json_obj}"
50
  raise gr.Error(warning_msg)
@@ -55,10 +56,9 @@ def predict(model_url, message, chatbot=[], system_prompt=""):
55
  error_msg = f"Failed to decode response as JSON: {str(e)}"
56
  raise gr.Error(error_msg)
57
 
58
-
59
- def test_preview_chatbot(message, history, model_url):
60
- response = predict(model_url, message, history, SYSTEM_PROMPT)
61
- text_start = response.rfind(" ", ) + len(" ")
62
  response = response[text_start:]
63
  return response
64
 
@@ -68,16 +68,7 @@ Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}
68
  "{EXAMPLE_INPUT}"
69
  """
70
 
71
- model_url_input = gr.Textbox(label="Model URL", value="https://huggingface.co/chat/models/meta-llama/Meta-Llama-3-70B-Instruct")
72
  chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
73
  textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
74
 
75
- demo = gr.Interface(
76
- fn=test_preview_chatbot,
77
- inputs=["text", "state", "text"],
78
- outputs="text",
79
- title=TITLE,
80
- description="Image Prompter"
81
- )
82
-
83
- demo.launch()
 
5
  SYSTEM_PROMPT = "As an LLM, your job is to generate detailed prompts that start with generate the image, for image generation models based on user input. Be descriptive and specific, but also make sure your prompts are clear and concise."
6
  TITLE = "Image Prompter"
7
  EXAMPLE_INPUT = "A Man Riding A Horse in Space"
8
+ zephyr_3b = "https://huggingface.co/chat/models/llama/llama-3b"
9
 
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
 
14
  """
15
  Constructs the input prompt string from the chatbot interactions and the current message.
16
  """
17
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
18
  for interaction in chatbot:
19
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
20
 
21
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
22
  return input_prompt
23
 
24
 
25
+ def post_request_beta(payload):
26
  """
27
+ Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
28
  """
29
+ response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
30
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
31
  return response.json()
32
 
33
 
34
+ def predict_beta(message, chatbot=[], system_prompt=""):
35
  input_prompt = build_input_prompt(message, chatbot, system_prompt)
36
  data = {
37
  "inputs": input_prompt
38
  }
39
 
40
  try:
41
+ response_data = post_request_beta(data)
42
  json_obj = response_data[0]
43
 
44
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
45
  bot_message = json_obj['generated_text']
46
  return bot_message
47
  elif 'error' in json_obj:
48
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
49
  else:
50
  warning_msg = f"Unexpected response: {json_obj}"
51
  raise gr.Error(warning_msg)
 
56
  error_msg = f"Failed to decode response as JSON: {str(e)}"
57
  raise gr.Error(error_msg)
58
 
59
+ def test_preview_chatbot(message, history):
60
+ response = predict_beta(message, history, SYSTEM_PROMPT)
61
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
 
62
  response = response[text_start:]
63
  return response
64
 
 
68
  "{EXAMPLE_INPUT}"
69
  """
70
 
 
71
  chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
72
  textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
73
 
74
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)