Update app.py
Browse files8 unmodified lines91011121314151617181920212223242526272829301 unmodified line32333435363738394041424344454647484950515224 unmodified lines777879808 unmodified lines# --- Agents ---def db_agent(query: str) -> str:conn = sqlite3.connect("shop.db")cur = conn.cursor()if "max revenue" in query.lower():cur.execute("""SELECT product, SUM(amount) AS revenueFROM transactionsWHERE date = date('now')GROUP BY productORDER BY revenue DESCLIMIT 1""")row = cur.fetchone()return f"Top product today: {row[0]} with ₹{row[1]:,.2f}" if row else "No transactions found for today."return Nonedef web_search_agent(query: str) -> str:1 unmodified lineresp = requests.get("https://serpapi.com/search",params={"q": query, "api_key": os.getenv("SERPAPI_KEY")}).json()snippet = resp.get("organic_results", [{}])[0].get("snippet", "")return llm_agent(f"Summarize: {snippet}")def llm_agent(prompt: str) -> str:resp = openai.ChatCompletion.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt}],temperature=0.2)return resp.choices[0].message.content.strip()def handle_query(query: str) -> str:24 unmodified linessubmit_btn.click(fn=handle_query, inputs=user_input, outputs=response_box)if __name__ == "__main__":demo.launch()8 unmodified lines91011121314151617181920212223242526272829303132333435361 unmodified line383940414243444546474849505152535455565758596024 unmodified lines858687888 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 revenueFROM transactionsWHERE date = date('now')GROUP BY productORDER BY revenue DESCLIMIT 1""")row = cur.fetchone()if row:return f"Top product today: {row[0]} with ₹{row[1]:,.2f}"return "No transactions found for today."return Noneexcept sqlite3.OperationalError as e:# Handle missing table or other DB errorsreturn f"Database error: {e}. Please initialize 'transactions' table in shop.db."def web_search_agent(query: str) -> str:1 unmodified lineresp = requests.get("https://serpapi.com/search",params={"q": query, "api_key": os.getenv("SERPAPI_KEY")})data = resp.json()snippet = data.get("organic_results", [{}])[0].get("snippet", "")return llm_agent(f"Summarize: {snippet}")def llm_agent(prompt: str) -> str:# Updated for openai>=1.0.0 interfaceresponse = openai.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt},],temperature=0.2,)return response.choices[0].message.content.strip()def handle_query(query: str) -> str:24 unmodified linessubmit_btn.click(fn=handle_query, inputs=user_input, outputs=response_box)if __name__ == "__main__":demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
app.py+31-23