Spaces:
Runtime error
Runtime error
TA
commited on
Commit
•
783f40e
1
Parent(s):
831bb79
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
-
from gradio import Error
|
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 |
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
12 |
|
13 |
def build_input_prompt(message, chatbot, system_prompt):
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return input_prompt
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
return response.json()
|
21 |
|
22 |
-
|
23 |
-
|
|
|
24 |
data = {
|
25 |
-
"
|
26 |
-
"max_new_tokens": 256,
|
27 |
-
"temperature": 0.7,
|
28 |
-
"top_p": 0.95
|
29 |
}
|
30 |
|
31 |
try:
|
32 |
-
response_data =
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
except requests.HTTPError as e:
|
36 |
error_msg = f"Request failed with status code {e.response.status_code}"
|
37 |
-
raise Error(error_msg)
|
38 |
except json.JSONDecodeError as e:
|
39 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
40 |
-
raise Error(error_msg)
|
41 |
|
42 |
-
def test_preview_chatbot(message):
|
43 |
-
|
44 |
-
|
|
|
45 |
return response
|
46 |
|
|
|
47 |
welcome_preview_message = f"""
|
48 |
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:
|
49 |
"{EXAMPLE_INPUT}"
|
@@ -52,12 +71,4 @@ Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}
|
|
52 |
chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
|
53 |
textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
|
54 |
|
55 |
-
demo = gr.
|
56 |
-
fn=test_preview_chatbot,
|
57 |
-
inputs="text",
|
58 |
-
outputs="text",
|
59 |
-
title=TITLE,
|
60 |
-
description="Image Prompter"
|
61 |
-
)
|
62 |
-
|
63 |
-
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 Man Riding A Horse in Space"
|
8 |
+
zephyr_7b_beta = "https://huggingface.co/chat/models/meta-llama/Meta-Llama-3-70B-Instruct"
|
9 |
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
12 |
|
13 |
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 = "<|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)
|
52 |
except requests.HTTPError as e:
|
53 |
error_msg = f"Request failed with status code {e.response.status_code}"
|
54 |
+
raise gr.Error(error_msg)
|
55 |
except json.JSONDecodeError as e:
|
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 |
|
65 |
+
|
66 |
welcome_preview_message = f"""
|
67 |
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:
|
68 |
"{EXAMPLE_INPUT}"
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|