TheMaisk commited on
Commit
8119b8e
·
verified ·
1 Parent(s): 2e521f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
10
+
11
+ HF_TOKEN = os.getenv("HF_TOKEN")
12
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
13
+
14
+ def build_input_prompt(message, chatbot, system_prompt):
15
+ """
16
+ Constructs the input prompt string from the chatbot interactions and the current message.
17
+ """
18
+ input_prompt = "\n" + system_prompt + "</s>\n\n"
19
+ for interaction in chatbot:
20
+ input_prompt += str(interaction[0]) + "</s>\n\n" + str(interaction[1]) + "\n</s>\n\n"
21
+
22
+ input_prompt += str(message) + "</s>\n"
23
+ return input_prompt
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()
31
+ return response.json()
32
+
33
+ def predict_beta(message, chatbot=[], system_prompt=""):
34
+ input_prompt = build_input_prompt(message, chatbot, system_prompt)
35
+ data = {"inputs": input_prompt}
36
+
37
+ try:
38
+ response_data = post_request_beta(data)
39
+ json_obj = response_data[0]
40
+
41
+ if 'generated_text' in json_obj and json_obj['generated_text']:
42
+ bot_message = json_obj['generated_text']
43
+ return bot_message
44
+ elif 'error' in json_obj:
45
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
46
+ else:
47
+ warning_msg = f"Unexpected response: {json_obj}"
48
+ raise gr.Error(warning_msg)
49
+ except requests.HTTPError as e:
50
+ error_msg = f"Request failed with status code {e.response.status_code}"
51
+ raise gr.Error(error_msg)
52
+ except json.JSONDecodeError as e:
53
+ error_msg = f"Failed to decode response as JSON: {str(e)}"
54
+ raise gr.Error(error_msg)
55
+
56
+ def test_preview_chatbot(message, history):
57
+ response = predict_beta(message, history, SYSTEM_PROMPT)
58
+ text_start = response.rfind("", ) + len("")
59
+ response = response[text_start:]
60
+ return response
61
+
62
+ welcome_preview_message = f"""
63
+ 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:
64
+ "{EXAMPLE_INPUT}"
65
+ """
66
+
67
+ css = """
68
+ body {
69
+ font-family: Arial, sans-serif;
70
+ }
71
+ .gradio-textbox {
72
+ border-radius: 10px;
73
+ border: 1px solid #cccccc;
74
+ }
75
+ .gradio-button {
76
+ background-color: #4CAF50;
77
+ color: white;
78
+ padding: 10px 20px;
79
+ border: none;
80
+ border-radius: 5px;
81
+ cursor: pointer;
82
+ }
83
+ .gradio-button:hover {
84
+ background-color: #45a049;
85
+ }
86
+ """
87
+
88
+ with gr.Blocks(css=css) as demo:
89
+ with gr.Row():
90
+ with gr.Column():
91
+ chatbot_preview = gr.Chatbot(label="Chatbot Preview", layout="panel", value=[(None, welcome_preview_message)])
92
+ with gr.Column():
93
+ textbox_preview = gr.Textbox(label="Your Input", scale=7, container=False, value=EXAMPLE_INPUT)
94
+ send_button = gr.Button("Send", elem_id="send_button")
95
+
96
+ chatbot_preview.update(test_preview_chatbot, inputs=[textbox_preview, chatbot_preview], outputs=chatbot_preview)
97
+ send_button.click(test_preview_chatbot, inputs=[textbox_preview, chatbot_preview], outputs=chatbot_preview)
98
+
99
+ demo.launch(share=True)