Update app.py
Browse files8 unmodified lines9101112121314151617181920212223242526271314151617181920212223242526272829303132333435361 unmodified line383940413536424344454647414849505152455354475556495758596024 unmodified lines85868780888 unmodified lines
# --- Agents ---def db_agent(query: str) -> str: try: conn = sqlite3.connect("shop.db") cur = conn.cursor() if "max revenue" in query.lower(): cur.execute( """ SELECT product, SUM(amount) AS revenue FROM transactions WHERE date = date('now') GROUP BY product ORDER BY revenue DESC LIMIT 1 """ ) row = cur.fetchone() return f"Top product today: {row[0]} with ₹{row[1]:,.2f}" if row else "No transactions found for today." return None conn = sqlite3.connect("shop.db") cur = conn.cursor() if "max revenue" in query.lower(): cur.execute( """ SELECT product, SUM(amount) AS revenue FROM transactions WHERE date = date('now') GROUP BY product ORDER BY revenue DESC LIMIT 1 """ ) row = cur.fetchone() if row: return f"Top product today: {row[0]} with ₹{row[1]:,.2f}" return "No transactions found for today." return None except sqlite3.OperationalError as e: # Handle missing table or other DB errors return f"Database error: {e}. Please initialize 'transactions' table in shop.db."
def web_search_agent(query: str) -> str:1 unmodified line resp = requests.get( "https://serpapi.com/search", params={"q": query, "api_key": os.getenv("SERPAPI_KEY")} ) ).json() snippet = resp.get("organic_results", [{}])[0].get("snippet", "") data = resp.json() snippet = data.get("organic_results", [{}])[0].get("snippet", "") return llm_agent(f"Summarize: {snippet}")
def llm_agent(prompt: str) -> str: resp = openai.ChatCompletion.create( # Updated for openai>=1.0.0 interface response = openai.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} {"role": "user", "content": prompt}, ], temperature=0.2 temperature=0.2, ) return resp.choices[0].message.content.strip() return response.choices[0].message.content.strip()
def handle_query(query: str) -> str:24 unmodified lines submit_btn.click(fn=handle_query, inputs=user_input, outputs=response_box)
if __name__ == "__main__": demo.launch() demo.launch(share=False, server_name="0.0.0.0", server_port=7860)app.py+31-23