TA commited on
Commit
fac7ca0
1 Parent(s): 65051e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,12 +1,22 @@
1
  import gradio as gr
2
  import os
3
  import requests
4
- import json
5
 
6
  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."
7
  TITLE = "Image Prompter"
8
- EXAMPLE_INPUT = "A Man Riding A Horse in Space"
9
- zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
 
 
 
 
 
 
 
 
 
 
 
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
12
 
@@ -14,11 +24,11 @@ def build_input_prompt(message, chatbot, system_prompt):
14
  """
15
  Constructs the input prompt string from the chatbot interactions and the current message.
16
  """
17
- input_prompt = "\n" + system_prompt + "</s>\n\n"
18
  for interaction in chatbot:
19
- input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
20
 
21
- input_prompt = input_prompt + str(message) + "</s>\n"
22
  return input_prompt
23
 
24
 
@@ -26,9 +36,6 @@ 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
- print(f"Sending payload: {payload}") # Debug print
30
- print(f"Headers: {HEADERS}") # Debug print
31
-
32
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
33
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
34
  return response.json()
@@ -42,8 +49,6 @@ def predict_beta(message, chatbot=[], system_prompt=""):
42
 
43
  try:
44
  response_data = post_request_beta(data)
45
- print(f"Response data: {response_data}") # Debug print
46
-
47
  json_obj = response_data[0]
48
 
49
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
@@ -63,11 +68,14 @@ def predict_beta(message, chatbot=[], system_prompt=""):
63
 
64
  def test_preview_chatbot(message, history):
65
  response = predict_beta(message, history, SYSTEM_PROMPT)
 
 
66
  return response
67
 
68
 
69
  welcome_preview_message = f"""
70
  Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}**!:\nThis is a chatbot that can generate detailed prompts for image generation models based on simple and short user input.\nSay something like:
 
71
  "{EXAMPLE_INPUT}"
72
  """
73
 
@@ -75,4 +83,4 @@ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_messa
75
  textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
76
 
77
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
78
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import requests
 
4
 
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 Reflective cat between stars."
8
+ import gradio as gr
9
+ import os
10
+ import requests
11
+
12
+ html_temp = """
13
+ <div style="position: absolute; top: 0; right: 0;">
14
+ <img src='https://huggingface.co/spaces/NerdN/open-gpt-Image-Prompter/blob/main/_45a03b4d-ea0f-4b81-873d-ff6b10461d52.jpg' alt='Your Image' style='width:100px;height:100px;'>
15
+ </div>
16
+ """
17
+
18
+ zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
19
+
20
  HF_TOKEN = os.getenv("HF_TOKEN")
21
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
22
 
 
24
  """
25
  Constructs the input prompt string from the chatbot interactions and the current message.
26
  """
27
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
28
  for interaction in chatbot:
29
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
30
 
31
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
32
  return input_prompt
33
 
34
 
 
36
  """
37
  Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
38
  """
 
 
 
39
  response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
40
  response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
41
  return response.json()
 
49
 
50
  try:
51
  response_data = post_request_beta(data)
 
 
52
  json_obj = response_data[0]
53
 
54
  if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
 
68
 
69
  def test_preview_chatbot(message, history):
70
  response = predict_beta(message, history, SYSTEM_PROMPT)
71
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
72
+ response = response[text_start:]
73
  return response
74
 
75
 
76
  welcome_preview_message = f"""
77
  Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}**!:\nThis is a chatbot that can generate detailed prompts for image generation models based on simple and short user input.\nSay something like:
78
+
79
  "{EXAMPLE_INPUT}"
80
  """
81
 
 
83
  textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
84
 
85
  demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
86
+ demo.launch(share=True)