Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from PIL import Image
|
|
8 |
import gradio as gr
|
9 |
from openai import OpenAI # Use the OpenAI client that supports multimodal messages
|
10 |
|
11 |
-
# Load API key from environment variable
|
12 |
HF_API_KEY = os.getenv("OPENAI_TOKEN")
|
13 |
if not HF_API_KEY:
|
14 |
raise ValueError("OPENAI_TOKEN environment variable not set")
|
@@ -82,7 +82,7 @@ def process_uploaded_file(file):
|
|
82 |
if file is None:
|
83 |
return "No file uploaded. Please upload a file."
|
84 |
|
85 |
-
#
|
86 |
if isinstance(file, dict):
|
87 |
file_path = file["name"]
|
88 |
else:
|
@@ -125,7 +125,7 @@ def clear_context():
|
|
125 |
# Predetermined Prompts
|
126 |
# -------------------------------
|
127 |
predetermined_prompts = {
|
128 |
-
|
129 |
"Software Tester": (
|
130 |
"Act as a software tester. Analyze the uploaded image of a software interface and generate comprehensive "
|
131 |
"test cases for its features. For each feature, provide test steps, expected results, and any necessary "
|
@@ -134,15 +134,14 @@ predetermined_prompts = {
|
|
134 |
}
|
135 |
|
136 |
# -------------------------------
|
137 |
-
# Chat Function
|
138 |
# -------------------------------
|
139 |
def chat_respond(user_message, history, prompt_option):
|
140 |
"""
|
141 |
-
Append the user message to the conversation history and
|
142 |
-
|
143 |
-
The history is a list of [user_text, assistant_text] pairs.
|
144 |
"""
|
145 |
-
# If this is the first message and
|
146 |
if history == []:
|
147 |
if not user_message.strip():
|
148 |
user_message = predetermined_prompts.get(prompt_option, "Hello")
|
@@ -151,11 +150,11 @@ def chat_respond(user_message, history, prompt_option):
|
|
151 |
|
152 |
history = history + [[user_message, ""]]
|
153 |
|
154 |
-
# Build the messages list for the multimodal API
|
155 |
messages = []
|
156 |
for i, (user_msg, assistant_msg) in enumerate(history):
|
157 |
user_content = [{"type": "text", "text": user_msg}]
|
158 |
-
# For the very first
|
159 |
if i == 0 and doc_state.current_doc_images:
|
160 |
buffered = io.BytesIO()
|
161 |
doc_state.current_doc_images[0].save(buffered, format="PNG")
|
@@ -172,10 +171,10 @@ def chat_respond(user_message, history, prompt_option):
|
|
172 |
"content": [{"type": "text", "text": assistant_msg}]
|
173 |
})
|
174 |
|
175 |
-
#
|
176 |
try:
|
177 |
stream = client.chat.completions.create(
|
178 |
-
model="google/gemini-2.0-
|
179 |
messages=messages,
|
180 |
max_tokens=8192,
|
181 |
stream=True
|
@@ -183,17 +182,15 @@ def chat_respond(user_message, history, prompt_option):
|
|
183 |
except Exception as e:
|
184 |
logger.error(f"Error calling the API: {str(e)}")
|
185 |
history[-1][1] = "An error occurred while processing your request. Please check your API credentials."
|
186 |
-
|
187 |
-
return
|
188 |
|
|
|
189 |
buffer = ""
|
190 |
for chunk in stream:
|
191 |
delta = chunk.choices[0].delta.content
|
192 |
buffer += delta
|
193 |
-
history[-1][1] = buffer
|
194 |
-
yield history, history
|
195 |
-
time.sleep(0.01)
|
196 |
|
|
|
197 |
return history, history
|
198 |
|
199 |
# -------------------------------
|
@@ -218,14 +215,15 @@ with gr.Blocks() as demo:
|
|
218 |
prompt_dropdown = gr.Dropdown(
|
219 |
label="Select Prompt",
|
220 |
choices=[
|
221 |
-
|
222 |
"Software Tester"
|
223 |
],
|
224 |
value="Software Tester"
|
225 |
)
|
226 |
clear_btn = gr.Button("Clear Document Context & Chat History")
|
227 |
|
228 |
-
|
|
|
229 |
|
230 |
with gr.Row():
|
231 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", show_label=False)
|
@@ -237,15 +235,14 @@ with gr.Blocks() as demo:
|
|
237 |
# When a file is uploaded, process it.
|
238 |
file_upload.change(fn=process_uploaded_file, inputs=file_upload, outputs=upload_status)
|
239 |
|
240 |
-
# Clear
|
241 |
clear_btn.click(fn=clear_context, outputs=[upload_status, chat_state])
|
242 |
|
243 |
# When the user clicks Send, process the message and update the chat.
|
244 |
send_btn.click(
|
245 |
fn=chat_respond,
|
246 |
inputs=[user_input, chat_state, prompt_dropdown],
|
247 |
-
outputs=[chatbot, chat_state]
|
248 |
-
stream=True
|
249 |
)
|
250 |
|
251 |
demo.launch(debug=True)
|
|
|
8 |
import gradio as gr
|
9 |
from openai import OpenAI # Use the OpenAI client that supports multimodal messages
|
10 |
|
11 |
+
# Load API key from environment variable
|
12 |
HF_API_KEY = os.getenv("OPENAI_TOKEN")
|
13 |
if not HF_API_KEY:
|
14 |
raise ValueError("OPENAI_TOKEN environment variable not set")
|
|
|
82 |
if file is None:
|
83 |
return "No file uploaded. Please upload a file."
|
84 |
|
85 |
+
# Gradio may pass a dict or a file-like object
|
86 |
if isinstance(file, dict):
|
87 |
file_path = file["name"]
|
88 |
else:
|
|
|
125 |
# Predetermined Prompts
|
126 |
# -------------------------------
|
127 |
predetermined_prompts = {
|
128 |
+
|
129 |
"Software Tester": (
|
130 |
"Act as a software tester. Analyze the uploaded image of a software interface and generate comprehensive "
|
131 |
"test cases for its features. For each feature, provide test steps, expected results, and any necessary "
|
|
|
134 |
}
|
135 |
|
136 |
# -------------------------------
|
137 |
+
# Chat Function (Non-streaming Version)
|
138 |
# -------------------------------
|
139 |
def chat_respond(user_message, history, prompt_option):
|
140 |
"""
|
141 |
+
Append the user message to the conversation history, call the API, and return the full response.
|
142 |
+
The conversation history is a list of [user_text, assistant_text] pairs.
|
|
|
143 |
"""
|
144 |
+
# If this is the first message and none is provided, use the predetermined prompt.
|
145 |
if history == []:
|
146 |
if not user_message.strip():
|
147 |
user_message = predetermined_prompts.get(prompt_option, "Hello")
|
|
|
150 |
|
151 |
history = history + [[user_message, ""]]
|
152 |
|
153 |
+
# Build the messages list for the multimodal API
|
154 |
messages = []
|
155 |
for i, (user_msg, assistant_msg) in enumerate(history):
|
156 |
user_content = [{"type": "text", "text": user_msg}]
|
157 |
+
# For the very first message, attach the image (if available)
|
158 |
if i == 0 and doc_state.current_doc_images:
|
159 |
buffered = io.BytesIO()
|
160 |
doc_state.current_doc_images[0].save(buffered, format="PNG")
|
|
|
171 |
"content": [{"type": "text", "text": assistant_msg}]
|
172 |
})
|
173 |
|
174 |
+
# Call the API (using stream=True internally but waiting for the full response)
|
175 |
try:
|
176 |
stream = client.chat.completions.create(
|
177 |
+
model="google/gemini-2.0-pro-exp-02-05:free",
|
178 |
messages=messages,
|
179 |
max_tokens=8192,
|
180 |
stream=True
|
|
|
182 |
except Exception as e:
|
183 |
logger.error(f"Error calling the API: {str(e)}")
|
184 |
history[-1][1] = "An error occurred while processing your request. Please check your API credentials."
|
185 |
+
return history, history
|
|
|
186 |
|
187 |
+
# Gather the full response from the streaming generator
|
188 |
buffer = ""
|
189 |
for chunk in stream:
|
190 |
delta = chunk.choices[0].delta.content
|
191 |
buffer += delta
|
|
|
|
|
|
|
192 |
|
193 |
+
history[-1][1] = buffer
|
194 |
return history, history
|
195 |
|
196 |
# -------------------------------
|
|
|
215 |
prompt_dropdown = gr.Dropdown(
|
216 |
label="Select Prompt",
|
217 |
choices=[
|
218 |
+
|
219 |
"Software Tester"
|
220 |
],
|
221 |
value="Software Tester"
|
222 |
)
|
223 |
clear_btn = gr.Button("Clear Document Context & Chat History")
|
224 |
|
225 |
+
# Set type='messages' to avoid deprecation warnings
|
226 |
+
chatbot = gr.Chatbot(label="Chat History", type="messages", elem_id="chatbot")
|
227 |
|
228 |
with gr.Row():
|
229 |
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", show_label=False)
|
|
|
235 |
# When a file is uploaded, process it.
|
236 |
file_upload.change(fn=process_uploaded_file, inputs=file_upload, outputs=upload_status)
|
237 |
|
238 |
+
# Clear document context and chat history.
|
239 |
clear_btn.click(fn=clear_context, outputs=[upload_status, chat_state])
|
240 |
|
241 |
# When the user clicks Send, process the message and update the chat.
|
242 |
send_btn.click(
|
243 |
fn=chat_respond,
|
244 |
inputs=[user_input, chat_state, prompt_dropdown],
|
245 |
+
outputs=[chatbot, chat_state]
|
|
|
246 |
)
|
247 |
|
248 |
demo.launch(debug=True)
|