arjunanand13 commited on
Commit
dd5aa4f
·
verified ·
1 Parent(s): 243aa91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -9,22 +9,28 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
9
 
10
  # --- Agents ---
11
  def db_agent(query: str) -> str:
12
- conn = sqlite3.connect("shop.db")
13
- cur = conn.cursor()
14
- if "max revenue" in query.lower():
15
- cur.execute(
16
- """
17
- SELECT product, SUM(amount) AS revenue
18
- FROM transactions
19
- WHERE date = date('now')
20
- GROUP BY product
21
- ORDER BY revenue DESC
22
- LIMIT 1
23
- """
24
- )
25
- row = cur.fetchone()
26
- return f"Top product today: {row[0]} with ₹{row[1]:,.2f}" if row else "No transactions found for today."
27
- return None
 
 
 
 
 
 
28
 
29
 
30
  def web_search_agent(query: str) -> str:
@@ -32,21 +38,23 @@ def web_search_agent(query: str) -> str:
32
  resp = requests.get(
33
  "https://serpapi.com/search",
34
  params={"q": query, "api_key": os.getenv("SERPAPI_KEY")}
35
- ).json()
36
- snippet = resp.get("organic_results", [{}])[0].get("snippet", "")
 
37
  return llm_agent(f"Summarize: {snippet}")
38
 
39
 
40
  def llm_agent(prompt: str) -> str:
41
- resp = openai.ChatCompletion.create(
 
42
  model="gpt-4o-mini",
43
  messages=[
44
  {"role": "system", "content": "You are a helpful assistant."},
45
- {"role": "user", "content": prompt}
46
  ],
47
- temperature=0.2
48
  )
49
- return resp.choices[0].message.content.strip()
50
 
51
 
52
  def handle_query(query: str) -> str:
@@ -77,4 +85,4 @@ with gr.Blocks() as demo:
77
  submit_btn.click(fn=handle_query, inputs=user_input, outputs=response_box)
78
 
79
  if __name__ == "__main__":
80
- demo.launch()
 
9
 
10
  # --- Agents ---
11
  def db_agent(query: str) -> str:
12
+ try:
13
+ conn = sqlite3.connect("shop.db")
14
+ cur = conn.cursor()
15
+ if "max revenue" in query.lower():
16
+ cur.execute(
17
+ """
18
+ SELECT product, SUM(amount) AS revenue
19
+ FROM transactions
20
+ WHERE date = date('now')
21
+ GROUP BY product
22
+ ORDER BY revenue DESC
23
+ LIMIT 1
24
+ """
25
+ )
26
+ row = cur.fetchone()
27
+ if row:
28
+ return f"Top product today: {row[0]} with ₹{row[1]:,.2f}"
29
+ return "No transactions found for today."
30
+ return None
31
+ except sqlite3.OperationalError as e:
32
+ # Handle missing table or other DB errors
33
+ return f"Database error: {e}. Please initialize 'transactions' table in shop.db."
34
 
35
 
36
  def web_search_agent(query: str) -> str:
 
38
  resp = requests.get(
39
  "https://serpapi.com/search",
40
  params={"q": query, "api_key": os.getenv("SERPAPI_KEY")}
41
+ )
42
+ data = resp.json()
43
+ snippet = data.get("organic_results", [{}])[0].get("snippet", "")
44
  return llm_agent(f"Summarize: {snippet}")
45
 
46
 
47
  def llm_agent(prompt: str) -> str:
48
+ # Updated for openai>=1.0.0 interface
49
+ response = openai.chat.completions.create(
50
  model="gpt-4o-mini",
51
  messages=[
52
  {"role": "system", "content": "You are a helpful assistant."},
53
+ {"role": "user", "content": prompt},
54
  ],
55
+ temperature=0.2,
56
  )
57
+ return response.choices[0].message.content.strip()
58
 
59
 
60
  def handle_query(query: str) -> str:
 
85
  submit_btn.click(fn=handle_query, inputs=user_input, outputs=response_box)
86
 
87
  if __name__ == "__main__":
88
+ demo.launch(share=False, server_name="0.0.0.0", server_port=7860)