Spaces:
Runtime error
Runtime error
TA
commited on
Commit
•
ebe55ae
1
Parent(s):
ff5d575
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,15 @@ 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 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
|
11 |
|
@@ -13,29 +20,41 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
13 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
14 |
|
15 |
def build_input_prompt(message, chatbot, system_prompt):
|
|
|
|
|
|
|
16 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
17 |
for interaction in chatbot:
|
18 |
input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
|
|
|
19 |
input_prompt = input_prompt + str(message) + "</s>\n"
|
20 |
return input_prompt
|
21 |
|
|
|
22 |
def post_request_beta(payload):
|
|
|
|
|
|
|
23 |
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
24 |
-
response.raise_for_status()
|
25 |
return response.json()
|
26 |
|
27 |
-
|
28 |
-
|
29 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
30 |
-
data = {
|
|
|
|
|
|
|
31 |
try:
|
32 |
response_data = post_request_beta(data)
|
33 |
json_obj = response_data[0]
|
|
|
34 |
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
35 |
bot_message = json_obj['generated_text']
|
36 |
return bot_message
|
37 |
elif 'error' in json_obj:
|
38 |
-
raise gr.Error(json_obj['error'] + ' Please refresh and try again with
|
39 |
else:
|
40 |
warning_msg = f"Unexpected response: {json_obj}"
|
41 |
raise gr.Error(warning_msg)
|
@@ -46,18 +65,22 @@ def predict_beta(message, chatbot=None, system_prompt=""):
|
|
46 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
47 |
raise gr.Error(error_msg)
|
48 |
|
49 |
-
def test_preview_chatbot(message, history
|
50 |
-
history = [] if history is None else history # Ensure history is not None
|
51 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
gr.Interface(
|
57 |
-
fn=test_preview_chatbot,
|
58 |
-
live=False,
|
59 |
-
examples=[[EXAMPLE_PROMPT]],
|
60 |
-
inputs=gr.Textbox(scale=7, container=False, value=EXAMPLE_PROMPT),
|
61 |
-
outputs=[gr.Textbox(), gr.Image()],
|
62 |
-
).launch(share=True)
|
63 |
|
|
|
|
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 |
+
|
9 |
+
html_temp = """
|
10 |
+
<div style="text-align: center;">
|
11 |
+
<h1>{}</h1>
|
12 |
+
<img src='https://huggingface.co/spaces/NerdN/open-gpt-Image-Prompter/blob/main/_45a03b4d-ea0f-4b81-873d-ff6b10461d52.jpg' alt='Your Image' style='width:300px;height:300px;'>
|
13 |
+
<p>{}</p>
|
14 |
+
</div>
|
15 |
+
""".format(TITLE, EXAMPLE_INPUT)
|
16 |
|
17 |
zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
|
18 |
|
|
|
20 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
21 |
|
22 |
def build_input_prompt(message, chatbot, system_prompt):
|
23 |
+
"""
|
24 |
+
Constructs the input prompt string from the chatbot interactions and the current message.
|
25 |
+
"""
|
26 |
input_prompt = "\n" + system_prompt + "</s>\n\n"
|
27 |
for interaction in chatbot:
|
28 |
input_prompt = input_prompt + str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
|
29 |
+
|
30 |
input_prompt = input_prompt + str(message) + "</s>\n"
|
31 |
return input_prompt
|
32 |
|
33 |
+
|
34 |
def post_request_beta(payload):
|
35 |
+
"""
|
36 |
+
Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
|
37 |
+
"""
|
38 |
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
39 |
+
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
40 |
return response.json()
|
41 |
|
42 |
+
|
43 |
+
def predict_beta(message, chatbot=[], system_prompt=""):
|
44 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
45 |
+
data = {
|
46 |
+
"inputs": input_prompt
|
47 |
+
}
|
48 |
+
|
49 |
try:
|
50 |
response_data = post_request_beta(data)
|
51 |
json_obj = response_data[0]
|
52 |
+
|
53 |
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
54 |
bot_message = json_obj['generated_text']
|
55 |
return bot_message
|
56 |
elif 'error' in json_obj:
|
57 |
+
raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
|
58 |
else:
|
59 |
warning_msg = f"Unexpected response: {json_obj}"
|
60 |
raise gr.Error(warning_msg)
|
|
|
65 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
66 |
raise gr.Error(error_msg)
|
67 |
|
68 |
+
def test_preview_chatbot(message, history):
|
|
|
69 |
response = predict_beta(message, history, SYSTEM_PROMPT)
|
70 |
+
text_start = response.rfind("", ) + len("")
|
71 |
+
response = response[text_start:]
|
72 |
+
return response
|
73 |
+
|
74 |
+
|
75 |
+
welcome_preview_message = f"""
|
76 |
+
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:
|
77 |
+
|
78 |
+
"{EXAMPLE_INPUT}"
|
79 |
+
"""
|
80 |
|
81 |
+
chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
|
82 |
+
textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
|
83 |
|
84 |
+
demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview, title=None, html=html_temp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
demo.launch(share=True)
|