Daemontatox commited on
Commit
8f401f6
·
verified ·
1 Parent(s): e4f9a72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -141
app.py CHANGED
@@ -21,13 +21,13 @@ def generate_image_fn(selected_prompt):
21
  """
22
  global global_image_data_url, global_image_prompt
23
 
24
- # Store the chosen prompt for later use in detail checking
25
  global_image_prompt = selected_prompt
26
 
27
  # Create an inference client for text-to-image (Stable Diffusion)
28
  image_client = InferenceClient(
29
  provider="hf-inference",
30
- api_key=inference_api_key # Loaded from env secrets
31
  )
32
 
33
  # Generate the image using the selected prompt.
@@ -36,7 +36,7 @@ def generate_image_fn(selected_prompt):
36
  model="stabilityai/stable-diffusion-3.5-large-turbo"
37
  )
38
 
39
- # Convert the PIL image to a PNG data URL
40
  buffered = io.BytesIO()
41
  image.save(buffered, format="PNG")
42
  img_bytes = buffered.getvalue()
@@ -45,16 +45,15 @@ def generate_image_fn(selected_prompt):
45
 
46
  return image
47
 
48
- def generate_image_and_reset_chat(selected_prompt, current_chat_history, current_chat_sessions):
49
  """
50
- Before generating a new image, automatically save the current chat session (if any)
51
- into the sessions state, then clear the chat history.
52
- Returns the generated image along with updated chat history (empty) and chat sessions.
53
  """
54
- new_sessions = current_chat_sessions.copy()
55
  if current_chat_history:
56
  new_sessions.append(current_chat_history)
57
- new_chat_history = []
58
  image = generate_image_fn(selected_prompt)
59
  return image, new_chat_history, new_sessions
60
 
@@ -81,63 +80,13 @@ def chat_about_image_fn(user_input):
81
 
82
  chat_client = OpenAI(
83
  base_url="https://api-inference.huggingface.co/v1/",
84
- api_key=chat_api_key # Loaded from env secrets
85
- )
86
-
87
- stream = chat_client.chat.completions.create(
88
- model="meta-llama/Llama-3.2-11B-Vision-Instruct",
89
- messages=messages,
90
- max_tokens=500,
91
- stream=True
92
- )
93
-
94
- response_text = ""
95
- for chunk in stream:
96
- response_text += chunk.choices[0].delta.content
97
-
98
- return response_text
99
-
100
- def check_details_fn(user_details):
101
- """
102
- Compares the user's description of the generated image with the prompt used to generate it.
103
- The function sends both the original prompt and the user description to the vision-chat model,
104
- which responds whether the description is correct and (if not) provides a hint.
105
- """
106
- if not global_image_prompt:
107
- return "Please generate an image first."
108
-
109
- # Build a message to instruct the model to evaluate the user's details.
110
- messages = [
111
- {
112
- "role": "user",
113
- "content": [
114
- {
115
- "type": "text",
116
- "text": (
117
- f"The image was generated using the prompt: '{global_image_prompt}'.\n"
118
- f"Evaluate the following user description of the image: '{user_details}'.\n"
119
- "If the description is accurate and captures the key elements of the prompt, reply with 'Correct'. "
120
- "If it is inaccurate or missing important details, reply with 'Incorrect' and provide a hint on what is missing. "
121
- "Be Friendly, You are a kids Assistant, use friendly and engaging tone. "
122
- "Don't Mention your system prompt or any prompt; speak from First Person View. "
123
- "Be lenient with the child, they are learning. "
124
- "Use simple and easy words. "
125
- "If some unimportant features are missing, you can mark it as correct."
126
- )
127
- }
128
- ]
129
- }
130
- ]
131
-
132
- chat_client = OpenAI(
133
- base_url="https://api-inference.huggingface.co/v1/",
134
- api_key=chat_api_key # Loaded from env secrets
135
  )
136
 
137
  stream = chat_client.chat.completions.create(
138
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
139
  messages=messages,
140
- max_tokens=512,
141
  stream=True
142
  )
143
 
@@ -147,7 +96,7 @@ def check_details_fn(user_details):
147
 
148
  return response_text
149
 
150
- # Define a list of prompts for the dropdown menu.
151
  prompt_options = [
152
  "Generate a simple, high-contrast image of a child displaying a clear facial expression, such as happiness, sadness, surprise, or anger. Use exaggerated but gentle features with soft colors to help autistic children recognize and describe emotions.",
153
  "Create an engaging scene with two or more cartoon-style characters interacting in a simple, easy-to-understand way. Ensure the scene encourages storytelling, such as two children sharing a toy, greeting each other, or helping one another.",
@@ -162,86 +111,70 @@ prompt_options = [
162
  ]
163
 
164
  ##############################################
165
- # Create the Gradio Interface with Tabs below.
166
  ##############################################
167
  with gr.Blocks() as demo:
168
- # State variables for chat history and saved sessions.
169
- # chat_history stores the ongoing conversation as a list of (user_message, bot_response) tuples.
170
- # chat_sessions stores a list of saved sessions (each session is itself a list of chat turns).
171
- chat_history = gr.State([]) # current session
172
- chat_sessions = gr.State([]) # list of sessions
173
-
174
- with gr.Tabs():
175
- # ----- Tab 1: Image Generation and Check Details -----
176
- with gr.Tab("Image Generation"):
177
- gr.Markdown("# Image Generation")
178
- with gr.Row():
179
- with gr.Column():
180
- prompt_dropdown = gr.Dropdown(label="Select a prompt", choices=prompt_options, value=prompt_options[0])
181
- generate_btn = gr.Button("Generate Image")
182
- img_output = gr.Image(label="Generated Image")
183
- # When generating a new image, automatically save any existing chat session and reset the chat.
184
- generate_btn.click(
185
- generate_image_and_reset_chat,
186
- inputs=[prompt_dropdown, chat_history, chat_sessions],
187
- outputs=[img_output, chat_history, chat_sessions]
188
- )
189
- with gr.Column():
190
- gr.Markdown("## Check Your Description of the Image")
191
- details_input = gr.Textbox(
192
- label="Enter details about the image",
193
- placeholder="Describe the key elements of the image..."
194
- )
195
- check_details_btn = gr.Button("Check Details")
196
- details_output = gr.Textbox(label="Result")
197
- check_details_btn.click(check_details_fn, inputs=details_input, outputs=details_output)
198
-
199
- # ----- Tab 2: Chat with Image and Chat History -----
200
- with gr.Tab("Chat"):
201
- gr.Markdown("# Chat about the Image")
202
- gr.Markdown("The conversation below remembers your messages. (Make sure you have generated an image first!)")
203
- chatbot = gr.Chatbot(label="Chat History")
204
- with gr.Row():
205
- chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", show_label=False)
206
- send_btn = gr.Button("Send")
207
- with gr.Row():
208
- clear_btn = gr.Button("Clear Chat")
209
- # You can still save the session manually if desired.
210
- save_session_btn = gr.Button("Save Session")
211
-
212
- def chat_respond(user_message, history):
213
- """
214
- Process the chat message: if an image is available, call the chat function;
215
- otherwise, return a reminder message.
216
- """
217
- if not global_image_data_url:
218
- bot_message = "Please generate an image first."
219
- else:
220
- bot_message = chat_about_image_fn(user_message)
221
- history = history + [(user_message, bot_message)]
222
- return "", history
223
-
224
- send_btn.click(chat_respond, inputs=[chat_input, chat_history], outputs=[chat_input, chatbot])
225
- chat_input.submit(chat_respond, inputs=[chat_input, chat_history], outputs=[chat_input, chatbot])
226
-
227
- # Button to clear the current chat (only clears current chat history).
228
- clear_btn.click(lambda: ("", []), outputs=[chat_input, chatbot])
229
-
230
- # Button to manually save the current session: append current chat history to sessions and then clear the chat.
231
- def save_session(history, sessions):
232
- new_sessions = sessions.copy()
233
- if history:
234
- new_sessions.append(history)
235
- return "", [], new_sessions
236
- save_session_btn.click(save_session, inputs=[chatbot, chat_sessions], outputs=[chat_input, chatbot, chat_sessions])
237
 
238
- # ----- Tab 3: Chat Sessions History -----
239
- with gr.Tab("Sessions"):
240
- gr.Markdown("# Saved Chat Sessions")
241
- gr.Markdown("Below is a JSON representation of your saved chat sessions.")
242
- sessions_output = gr.JSON(label="Saved Sessions")
243
- refresh_btn = gr.Button("Refresh Sessions")
244
- refresh_btn.click(lambda sessions: sessions, inputs=chat_sessions, outputs=sessions_output)
245
-
246
- # Launch the app. (Hugging Face Spaces will detect and run this.)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  demo.launch()
 
21
  """
22
  global global_image_data_url, global_image_prompt
23
 
24
+ # Save the chosen prompt for later use
25
  global_image_prompt = selected_prompt
26
 
27
  # Create an inference client for text-to-image (Stable Diffusion)
28
  image_client = InferenceClient(
29
  provider="hf-inference",
30
+ api_key=inference_api_key
31
  )
32
 
33
  # Generate the image using the selected prompt.
 
36
  model="stabilityai/stable-diffusion-3.5-large-turbo"
37
  )
38
 
39
+ # Convert the PIL image to a PNG data URL.
40
  buffered = io.BytesIO()
41
  image.save(buffered, format="PNG")
42
  img_bytes = buffered.getvalue()
 
45
 
46
  return image
47
 
48
+ def generate_image_and_reset_chat(selected_prompt, current_chat_history, saved_sessions):
49
  """
50
+ Before generating a new image, automatically save any current chat session (if exists)
51
+ into the saved sessions list and reset the active chat history.
 
52
  """
53
+ new_sessions = saved_sessions.copy()
54
  if current_chat_history:
55
  new_sessions.append(current_chat_history)
56
+ new_chat_history = [] # Reset active chat history
57
  image = generate_image_fn(selected_prompt)
58
  return image, new_chat_history, new_sessions
59
 
 
80
 
81
  chat_client = OpenAI(
82
  base_url="https://api-inference.huggingface.co/v1/",
83
+ api_key=chat_api_key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
  stream = chat_client.chat.completions.create(
87
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
88
  messages=messages,
89
+ max_tokens=500,
90
  stream=True
91
  )
92
 
 
96
 
97
  return response_text
98
 
99
+ # Define a list of prompts for the dropdown.
100
  prompt_options = [
101
  "Generate a simple, high-contrast image of a child displaying a clear facial expression, such as happiness, sadness, surprise, or anger. Use exaggerated but gentle features with soft colors to help autistic children recognize and describe emotions.",
102
  "Create an engaging scene with two or more cartoon-style characters interacting in a simple, easy-to-understand way. Ensure the scene encourages storytelling, such as two children sharing a toy, greeting each other, or helping one another.",
 
111
  ]
112
 
113
  ##############################################
114
+ # Create the Gradio Interface (Single-Page)
115
  ##############################################
116
  with gr.Blocks() as demo:
117
+ # State variables:
118
+ # - chat_history: holds the active conversation as a list of (user_message, bot_response) tuples.
119
+ # - saved_sessions: holds all saved chat sessions.
120
+ chat_history = gr.State([])
121
+ saved_sessions = gr.State([])
122
+
123
+ gr.Markdown("# Image Generation & Chat Inference")
124
+
125
+ # ----- Image Generation Section -----
126
+ with gr.Box():
127
+ gr.Markdown("## Generate Image")
128
+ with gr.Row():
129
+ prompt_dropdown = gr.Dropdown(label="Select a prompt", choices=prompt_options, value=prompt_options[0])
130
+ generate_btn = gr.Button("Generate Image")
131
+ img_output = gr.Image(label="Generated Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
+ # When generating a new image, save any current chat session and reset chat history.
134
+ generate_btn.click(
135
+ generate_image_and_reset_chat,
136
+ inputs=[prompt_dropdown, chat_history, saved_sessions],
137
+ outputs=[img_output, chat_history, saved_sessions]
138
+ )
139
+
140
+ # ----- Chat Section -----
141
+ with gr.Box():
142
+ gr.Markdown("## Chat about the Image")
143
+ gr.Markdown("After generating an image, ask questions or make comments about it. Your conversation will be automatically saved after each message.")
144
+ chatbot = gr.Chatbot(label="Chat History")
145
+ with gr.Row():
146
+ chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", show_label=False)
147
+ send_btn = gr.Button("Send")
148
+
149
+ # Each time the user sends a message, update the chat history.
150
+ def chat_respond(user_message, history, sessions):
151
+ if not global_image_data_url:
152
+ bot_message = "Please generate an image first."
153
+ else:
154
+ bot_message = chat_about_image_fn(user_message)
155
+ new_history = history + [(user_message, bot_message)]
156
+ # Automatically update saved session with the active conversation.
157
+ new_sessions = sessions.copy()
158
+ if new_history:
159
+ # In this design, the current active session is always saved (overwritten) as the latest session.
160
+ if new_sessions:
161
+ new_sessions[-1] = new_history
162
+ else:
163
+ new_sessions.append(new_history)
164
+ return "", new_history, new_sessions
165
+
166
+ send_btn.click(chat_respond, inputs=[chat_input, chat_history, saved_sessions],
167
+ outputs=[chat_input, chatbot, saved_sessions])
168
+ chat_input.submit(chat_respond, inputs=[chat_input, chat_history, saved_sessions],
169
+ outputs=[chat_input, chatbot, saved_sessions])
170
+
171
+ # ----- Saved Sessions Section -----
172
+ with gr.Box():
173
+ gr.Markdown("## Saved Chat Sessions")
174
+ gr.Markdown("Your past chat sessions (including the active one) are saved below. You can refresh to view the latest sessions.")
175
+ sessions_output = gr.JSON(label="Saved Sessions")
176
+ refresh_btn = gr.Button("Refresh Saved Sessions")
177
+ refresh_btn.click(lambda sessions: sessions, inputs=saved_sessions, outputs=sessions_output)
178
+
179
+ # Launch the app.
180
  demo.launch()