Update app.py
Browse files
app.py
CHANGED
|
@@ -7,11 +7,35 @@ import asyncio
|
|
| 7 |
from gtts import gTTS
|
| 8 |
from typing_extensions import TypedDict
|
| 9 |
from langgraph.graph import StateGraph, START, END
|
|
|
|
| 10 |
|
| 11 |
# Load API keys
|
| 12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# --- Business Logic Functions ---
|
|
|
|
| 15 |
def db_agent(query: str) -> str:
|
| 16 |
try:
|
| 17 |
conn = sqlite3.connect("shop.db")
|
|
@@ -31,7 +55,8 @@ def db_agent(query: str) -> str:
|
|
| 31 |
return f"Top product today: {row[0]} with ₹{row[1]:,.2f}"
|
| 32 |
return "No transactions found for today."
|
| 33 |
except sqlite3.OperationalError as e:
|
| 34 |
-
return f"Database error: {e}. Please
|
|
|
|
| 35 |
|
| 36 |
def web_search_agent(query: str) -> str:
|
| 37 |
try:
|
|
@@ -46,6 +71,7 @@ def web_search_agent(query: str) -> str:
|
|
| 46 |
pass
|
| 47 |
return llm_agent(query)
|
| 48 |
|
|
|
|
| 49 |
def llm_agent(query: str) -> str:
|
| 50 |
response = openai.chat.completions.create(
|
| 51 |
model="gpt-4o-mini",
|
|
@@ -57,6 +83,7 @@ def llm_agent(query: str) -> str:
|
|
| 57 |
)
|
| 58 |
return response.choices[0].message.content.strip()
|
| 59 |
|
|
|
|
| 60 |
def stt_agent(audio_path: str) -> str:
|
| 61 |
with open(audio_path, "rb") as afile:
|
| 62 |
transcript = openai.audio.transcriptions.create(
|
|
@@ -65,6 +92,7 @@ def stt_agent(audio_path: str) -> str:
|
|
| 65 |
)
|
| 66 |
return transcript.text.strip()
|
| 67 |
|
|
|
|
| 68 |
def tts_agent(text: str, lang: str = 'en') -> str:
|
| 69 |
tts = gTTS(text=text, lang=lang)
|
| 70 |
out_path = "response_audio.mp3"
|
|
@@ -77,6 +105,7 @@ class State(TypedDict):
|
|
| 77 |
result: str
|
| 78 |
|
| 79 |
# Routing logic based on query
|
|
|
|
| 80 |
def route_fn(state: State) -> str:
|
| 81 |
q = state["query"].lower()
|
| 82 |
if any(k in q for k in ["max revenue", "revenue"]):
|
|
@@ -135,7 +164,7 @@ def handle_query(audio_or_text: str):
|
|
| 135 |
# --- Gradio UI ---
|
| 136 |
with gr.Blocks() as demo:
|
| 137 |
gr.Markdown("## Shop Voice-Box Assistant (Speech In/Out)")
|
| 138 |
-
inp = gr.Audio(sources=["microphone"], type="filepath", label="Speak or type your question")
|
| 139 |
out_text = gr.Textbox(label="Answer (text)")
|
| 140 |
out_audio = gr.Audio(label="Answer (speech)")
|
| 141 |
submit = gr.Button("Submit")
|
|
|
|
| 7 |
from gtts import gTTS
|
| 8 |
from typing_extensions import TypedDict
|
| 9 |
from langgraph.graph import StateGraph, START, END
|
| 10 |
+
import csv
|
| 11 |
|
| 12 |
# Load API keys
|
| 13 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
|
| 15 |
+
# --- Initialize Database from external CSV ---
|
| 16 |
+
def init_db_from_csv(csv_path: str = "transactions.csv") -> None:
|
| 17 |
+
"""Create 'transactions' table and load data from the provided CSV file."""
|
| 18 |
+
conn = sqlite3.connect("shop.db")
|
| 19 |
+
cur = conn.cursor()
|
| 20 |
+
cur.execute(
|
| 21 |
+
"CREATE TABLE IF NOT EXISTS transactions (date TEXT, product TEXT, amount REAL)"
|
| 22 |
+
)
|
| 23 |
+
with open(csv_path, newline='') as f:
|
| 24 |
+
reader = csv.DictReader(f)
|
| 25 |
+
rows = [(row["date"], row["product"], float(row["amount"])) for row in reader]
|
| 26 |
+
# Replace old data
|
| 27 |
+
cur.execute("DELETE FROM transactions")
|
| 28 |
+
cur.executemany(
|
| 29 |
+
"INSERT INTO transactions (date, product, amount) VALUES (?, ?, ?)", rows
|
| 30 |
+
)
|
| 31 |
+
conn.commit()
|
| 32 |
+
conn.close()
|
| 33 |
+
|
| 34 |
+
# Initialize DB at startup (ensure transactions.csv is present)
|
| 35 |
+
init_db_from_csv()
|
| 36 |
+
|
| 37 |
# --- Business Logic Functions ---
|
| 38 |
+
|
| 39 |
def db_agent(query: str) -> str:
|
| 40 |
try:
|
| 41 |
conn = sqlite3.connect("shop.db")
|
|
|
|
| 55 |
return f"Top product today: {row[0]} with ₹{row[1]:,.2f}"
|
| 56 |
return "No transactions found for today."
|
| 57 |
except sqlite3.OperationalError as e:
|
| 58 |
+
return f"Database error: {e}. Please check 'transactions' table in shop.db."
|
| 59 |
+
|
| 60 |
|
| 61 |
def web_search_agent(query: str) -> str:
|
| 62 |
try:
|
|
|
|
| 71 |
pass
|
| 72 |
return llm_agent(query)
|
| 73 |
|
| 74 |
+
|
| 75 |
def llm_agent(query: str) -> str:
|
| 76 |
response = openai.chat.completions.create(
|
| 77 |
model="gpt-4o-mini",
|
|
|
|
| 83 |
)
|
| 84 |
return response.choices[0].message.content.strip()
|
| 85 |
|
| 86 |
+
|
| 87 |
def stt_agent(audio_path: str) -> str:
|
| 88 |
with open(audio_path, "rb") as afile:
|
| 89 |
transcript = openai.audio.transcriptions.create(
|
|
|
|
| 92 |
)
|
| 93 |
return transcript.text.strip()
|
| 94 |
|
| 95 |
+
|
| 96 |
def tts_agent(text: str, lang: str = 'en') -> str:
|
| 97 |
tts = gTTS(text=text, lang=lang)
|
| 98 |
out_path = "response_audio.mp3"
|
|
|
|
| 105 |
result: str
|
| 106 |
|
| 107 |
# Routing logic based on query
|
| 108 |
+
|
| 109 |
def route_fn(state: State) -> str:
|
| 110 |
q = state["query"].lower()
|
| 111 |
if any(k in q for k in ["max revenue", "revenue"]):
|
|
|
|
| 164 |
# --- Gradio UI ---
|
| 165 |
with gr.Blocks() as demo:
|
| 166 |
gr.Markdown("## Shop Voice-Box Assistant (Speech In/Out)")
|
| 167 |
+
inp = gr.Audio(sources=["microphone"], type="filepath", label="Speak or type your question or upload transactions.csv separately in root")
|
| 168 |
out_text = gr.Textbox(label="Answer (text)")
|
| 169 |
out_audio = gr.Audio(label="Answer (speech)")
|
| 170 |
submit = gr.Button("Submit")
|