Spaces:
Runtime error
Runtime error
TA
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ 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 |
-
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,41 +13,32 @@ 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 =
|
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
|
26 |
"""
|
27 |
-
Sends a POST request to the
|
28 |
"""
|
29 |
-
response = requests.post(
|
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
|
35 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
36 |
data = {
|
37 |
-
"
|
|
|
|
|
|
|
38 |
}
|
39 |
|
40 |
try:
|
41 |
-
response_data =
|
42 |
-
|
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)
|
@@ -56,10 +46,10 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
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 |
-
|
61 |
-
|
62 |
-
response = response[text_start:]
|
63 |
return response
|
64 |
|
65 |
|
@@ -71,4 +61,12 @@ Expand your imagination and broaden your horizons with LLM. Welcome to **{TITLE}
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
"""
|
14 |
Constructs the input prompt string from the chatbot interactions and the current message.
|
15 |
"""
|
16 |
+
input_prompt = system_prompt + "\n\n" + message
|
|
|
|
|
|
|
|
|
17 |
return input_prompt
|
18 |
|
19 |
|
20 |
+
def post_request(model_url, payload):
|
21 |
"""
|
22 |
+
Sends a POST request to the specified model URL and returns the JSON response.
|
23 |
"""
|
24 |
+
response = requests.post(model_url, headers=HEADERS, json=payload)
|
25 |
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
26 |
return response.json()
|
27 |
|
28 |
|
29 |
+
def predict(model_url, message, chatbot=[], system_prompt=""):
|
30 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
31 |
data = {
|
32 |
+
"prompt": input_prompt,
|
33 |
+
"max_new_tokens": 256,
|
34 |
+
"temperature": 0.7,
|
35 |
+
"top_p": 0.95
|
36 |
}
|
37 |
|
38 |
try:
|
39 |
+
response_data = post_request(model_url, data)
|
40 |
+
bot_message = response_data["generated_text"]
|
41 |
+
return bot_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except requests.HTTPError as e:
|
43 |
error_msg = f"Request failed with status code {e.response.status_code}"
|
44 |
raise gr.Error(error_msg)
|
|
|
46 |
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
47 |
raise gr.Error(error_msg)
|
48 |
|
49 |
+
|
50 |
def test_preview_chatbot(message, history):
|
51 |
+
model_url = "https://huggingface.co/chat/models/llama/llama-3b"
|
52 |
+
response = predict(model_url, message, history, SYSTEM_PROMPT)
|
|
|
53 |
return response
|
54 |
|
55 |
|
|
|
61 |
chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
|
62 |
textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
|
63 |
|
64 |
+
demo = gr.Interface(
|
65 |
+
fn=test_preview_chatbot,
|
66 |
+
inputs=["text", "state"],
|
67 |
+
outputs="text",
|
68 |
+
title=TITLE,
|
69 |
+
description="Image Prompter"
|
70 |
+
)
|
71 |
+
|
72 |
+
demo.launch()
|