KheemDH commited on
Commit
6506053
·
1 Parent(s): 8a38f71

Updated APP

Browse files
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -19,7 +19,6 @@ class State(TypedDict):
19
  category: str
20
  sentiment: str
21
  response: str
22
- api_key: str
23
 
24
  def init_llm(api_key: str):
25
  os.environ["OPENAI_API_KEY"] = api_key
@@ -175,21 +174,24 @@ workflow.add_edge("escalate", END)
175
  workflow.set_entry_point("categorize")
176
  app = workflow.compile()
177
 
178
- def run_customer_support(query: str, api_key: str) -> str:
179
- """
180
- Process the customer query through the workflow.
181
- Use the provided API key, or if none is given, fall back to the .env value.
182
- Only the final answer is returned.
183
- """
184
- # If no API key is provided in the UI, try to read it from the environment.
185
- if not api_key.strip():
186
- api_key = os.getenv("OPENAI_API_KEY", "")
187
- if not api_key:
188
- return "Please provide a valid OpenAI API key."
189
- os.environ["OPENAI_API_KEY"] = api_key
190
- results = app.invoke({"query": query})
191
- # Return only the final answer (the response part)
192
- return results.get("response", "No response generated.")
 
 
 
193
 
194
  # Build the Gradio UI.
195
  with gr.Blocks(title="Customer Support Agent with Browser Use") as demo:
@@ -198,14 +200,28 @@ with gr.Blocks(title="Customer Support Agent with Browser Use") as demo:
198
 
199
  with gr.Row():
200
  with gr.Column():
201
- # The API key textbox (if left empty, the app will try to use the .env key)
202
- api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", value="")
203
- query_input = gr.Textbox(label="Customer Query", placeholder="Enter your query here...", lines=3)
 
 
 
 
 
 
 
204
  submit_btn = gr.Button("Submit Query")
205
  with gr.Column():
206
- output_box = gr.Textbox(label="Agent Response", lines=10, interactive=False)
 
 
 
 
207
 
208
- submit_btn.click(fn=run_customer_support, inputs=[query_input, api_key_input], outputs=output_box)
209
-
 
 
 
210
  if __name__ == "__main__":
211
  demo.launch()
 
19
  category: str
20
  sentiment: str
21
  response: str
 
22
 
23
  def init_llm(api_key: str):
24
  os.environ["OPENAI_API_KEY"] = api_key
 
174
  workflow.set_entry_point("categorize")
175
  app = workflow.compile()
176
 
177
+ async def run_customer_support(query: str, api_key: str = "") -> str:
178
+ if not api_key and not os.getenv("OPENAI_API_KEY"):
179
+ return "Error: Please provide an OpenAI API key"
180
+
181
+ try:
182
+ if api_key:
183
+ os.environ["OPENAI_API_KEY"] = api_key
184
+
185
+ llm = ChatOpenAI(temperature=0)
186
+ agent = Agent(
187
+ task=query,
188
+ llm=llm,
189
+ )
190
+ result = await agent.run()
191
+ return result
192
+ except Exception as e:
193
+ return f"Error: {str(e)}"
194
+
195
 
196
  # Build the Gradio UI.
197
  with gr.Blocks(title="Customer Support Agent with Browser Use") as demo:
 
200
 
201
  with gr.Row():
202
  with gr.Column():
203
+ api_key_input = gr.Textbox(
204
+ label="OpenAI API Key",
205
+ type="password",
206
+ placeholder="sk-..."
207
+ )
208
+ query_input = gr.Textbox(
209
+ label="Customer Query",
210
+ placeholder="Enter your query here...",
211
+ lines=3
212
+ )
213
  submit_btn = gr.Button("Submit Query")
214
  with gr.Column():
215
+ output_box = gr.Textbox(
216
+ label="Agent Response",
217
+ lines=10,
218
+ interactive=False
219
+ )
220
 
221
+ submit_btn.click(
222
+ fn=run_customer_support,
223
+ inputs=[query_input, api_key_input],
224
+ outputs=output_box
225
+ )
226
  if __name__ == "__main__":
227
  demo.launch()