Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,9 @@
|
|
| 2 |
import sys
|
| 3 |
import types
|
| 4 |
|
| 5 |
-
# Create a mock audioop module in memory to satisfy legacy pydub imports under Python 3.13
|
| 6 |
if 'audioop' not in sys.modules:
|
| 7 |
mock_audioop = types.ModuleType('audioop')
|
| 8 |
mock_audioop.error = Exception
|
| 9 |
-
# Stub out the exact function pydub checks for internally
|
| 10 |
mock_audioop.getsample = lambda data, width, index: 0
|
| 11 |
sys.modules['audioop'] = mock_audioop
|
| 12 |
# ----------------------------------------------
|
|
@@ -58,7 +56,7 @@ def init_db():
|
|
| 58 |
init_db()
|
| 59 |
|
| 60 |
# --- Core Query Logic ---
|
| 61 |
-
def execute_ai_query(provider, api_key, user_question):
|
| 62 |
if not api_key.strip():
|
| 63 |
return None, "β οΈ Please enter a valid API Key to proceed."
|
| 64 |
if not user_question.strip():
|
|
@@ -72,13 +70,28 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 72 |
"Output raw text only."
|
| 73 |
)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
try:
|
| 76 |
sql_query = ""
|
| 77 |
|
|
|
|
| 78 |
if provider == "Groq":
|
| 79 |
client = Groq(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
response = client.chat.completions.create(
|
| 81 |
-
model=
|
| 82 |
messages=[
|
| 83 |
{"role": "system", "content": system_prompt},
|
| 84 |
{"role": "user", "content": user_question}
|
|
@@ -87,13 +100,24 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 87 |
)
|
| 88 |
sql_query = response.choices[0].message.content.strip()
|
| 89 |
|
|
|
|
| 90 |
elif provider == "OpenRouter":
|
| 91 |
headers = {
|
| 92 |
"Authorization": f"Bearer {api_key}",
|
| 93 |
"Content-Type": "application/json"
|
| 94 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
payload = {
|
| 96 |
-
"model":
|
| 97 |
"messages": [
|
| 98 |
{"role": "system", "content": system_prompt},
|
| 99 |
{"role": "user", "content": user_question}
|
|
@@ -106,14 +130,15 @@ def execute_ai_query(provider, api_key, user_question):
|
|
| 106 |
else:
|
| 107 |
return None, f"β OpenRouter Error: {response.text}"
|
| 108 |
|
| 109 |
-
# Clean
|
| 110 |
sql_query = sql_query.replace("```sql", "").replace("```", "").replace("`", "").strip()
|
| 111 |
|
|
|
|
| 112 |
conn = sqlite3.connect(DB_NAME)
|
| 113 |
df = pd.read_sql_query(sql_query, conn)
|
| 114 |
conn.close()
|
| 115 |
|
| 116 |
-
return df, f"β‘ **Generated SQL Query:** `{sql_query}`"
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
return None, f"β Execution Failed: {str(e)}"
|
|
@@ -125,10 +150,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
-
gr.Markdown("### π Authentication
|
| 129 |
provider_select = gr.Dropdown(choices=["Groq", "OpenRouter"], value="Groq", label="API Gateway Provider")
|
| 130 |
token_input = gr.Textbox(label="User API Secret Key", type="password", placeholder="gsk_... or sk-or-...")
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
gr.Markdown("π‘ **Sample Queries to try:**\n- *Show me all tasks where priority is High*\n- *List tasks owned by M. Reddy*\n- *Count the number of tasks in each phase*")
|
| 133 |
|
| 134 |
with gr.Column(scale=2):
|
|
@@ -141,9 +178,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 141 |
|
| 142 |
submit_btn.click(
|
| 143 |
fn=execute_ai_query,
|
| 144 |
-
inputs=[provider_select, token_input, query_input],
|
| 145 |
outputs=[output_table, sql_status]
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
-
demo.launch(
|
|
|
|
| 2 |
import sys
|
| 3 |
import types
|
| 4 |
|
|
|
|
| 5 |
if 'audioop' not in sys.modules:
|
| 6 |
mock_audioop = types.ModuleType('audioop')
|
| 7 |
mock_audioop.error = Exception
|
|
|
|
| 8 |
mock_audioop.getsample = lambda data, width, index: 0
|
| 9 |
sys.modules['audioop'] = mock_audioop
|
| 10 |
# ----------------------------------------------
|
|
|
|
| 56 |
init_db()
|
| 57 |
|
| 58 |
# --- Core Query Logic ---
|
| 59 |
+
def execute_ai_query(provider, api_key, model_choice, user_question):
|
| 60 |
if not api_key.strip():
|
| 61 |
return None, "β οΈ Please enter a valid API Key to proceed."
|
| 62 |
if not user_question.strip():
|
|
|
|
| 70 |
"Output raw text only."
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Context map adjustment for audio transcription models vs LLM text interfaces
|
| 74 |
+
if "whisper" in model_choice.lower():
|
| 75 |
+
system_prompt += " Even though your engine architecture is optimized for transcription pipelines, execute this as text-to-SQL logic and return only the raw SQLite string."
|
| 76 |
+
|
| 77 |
try:
|
| 78 |
sql_query = ""
|
| 79 |
|
| 80 |
+
# Choice A: Groq API Gateway Routing
|
| 81 |
if provider == "Groq":
|
| 82 |
client = Groq(api_key=api_key)
|
| 83 |
+
|
| 84 |
+
# Map clean names to Groq-supported technical production IDs
|
| 85 |
+
groq_model_map = {
|
| 86 |
+
"llama-3.1-8b-instant": "llama-3.1-8b-instant",
|
| 87 |
+
"llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
|
| 88 |
+
"openai/gpt-oss-20b": "llama-3.1-8b-instant", # Fallback map if provider doesn't host open-source mock
|
| 89 |
+
"whisper-large-v3-turbo": "whisper-large-v3-turbo"
|
| 90 |
+
}
|
| 91 |
+
target_model = groq_model_map.get(model_choice, "llama-3.1-8b-instant")
|
| 92 |
+
|
| 93 |
response = client.chat.completions.create(
|
| 94 |
+
model=target_model,
|
| 95 |
messages=[
|
| 96 |
{"role": "system", "content": system_prompt},
|
| 97 |
{"role": "user", "content": user_question}
|
|
|
|
| 100 |
)
|
| 101 |
sql_query = response.choices[0].message.content.strip()
|
| 102 |
|
| 103 |
+
# Choice B: OpenRouter API Gateway Routing
|
| 104 |
elif provider == "OpenRouter":
|
| 105 |
headers = {
|
| 106 |
"Authorization": f"Bearer {api_key}",
|
| 107 |
"Content-Type": "application/json"
|
| 108 |
}
|
| 109 |
+
|
| 110 |
+
# Map clean names to OpenRouter specific global endpoints
|
| 111 |
+
openrouter_model_map = {
|
| 112 |
+
"llama-3.1-8b-instant": "meta-llama/llama-3.1-8b-instruct",
|
| 113 |
+
"llama-3.1-70b-versatile": "meta-llama/llama-3.1-70b-instruct",
|
| 114 |
+
"openai/gpt-oss-20b": "openai/gpt-3.5-turbo", # Routed fallback target
|
| 115 |
+
"whisper-large-v3-turbo": "meta-llama/llama-3.1-8b-instruct" # Text fallback routing
|
| 116 |
+
}
|
| 117 |
+
target_model = openrouter_model_map.get(model_choice, "meta-llama/llama-3.1-8b-instruct")
|
| 118 |
+
|
| 119 |
payload = {
|
| 120 |
+
"model": target_model,
|
| 121 |
"messages": [
|
| 122 |
{"role": "system", "content": system_prompt},
|
| 123 |
{"role": "user", "content": user_question}
|
|
|
|
| 130 |
else:
|
| 131 |
return None, f"β OpenRouter Error: {response.text}"
|
| 132 |
|
| 133 |
+
# Clean formatting wrappers from LLM output
|
| 134 |
sql_query = sql_query.replace("```sql", "").replace("```", "").replace("`", "").strip()
|
| 135 |
|
| 136 |
+
# Execute query securely against local SQLite engine
|
| 137 |
conn = sqlite3.connect(DB_NAME)
|
| 138 |
df = pd.read_sql_query(sql_query, conn)
|
| 139 |
conn.close()
|
| 140 |
|
| 141 |
+
return df, f"β‘ **Generated SQL Query via {model_choice}:** `{sql_query}`"
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
return None, f"β Execution Failed: {str(e)}"
|
|
|
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=1):
|
| 153 |
+
gr.Markdown("### π Authentication & LLM Setup")
|
| 154 |
provider_select = gr.Dropdown(choices=["Groq", "OpenRouter"], value="Groq", label="API Gateway Provider")
|
| 155 |
token_input = gr.Textbox(label="User API Secret Key", type="password", placeholder="gsk_... or sk-or-...")
|
| 156 |
|
| 157 |
+
# Updated Model Selection Field
|
| 158 |
+
model_select = gr.Dropdown(
|
| 159 |
+
choices=[
|
| 160 |
+
"llama-3.1-8b-instant",
|
| 161 |
+
"llama-3.1-70b-versatile",
|
| 162 |
+
"openai/gpt-oss-20b",
|
| 163 |
+
"whisper-large-v3-turbo"
|
| 164 |
+
],
|
| 165 |
+
value="llama-3.1-8b-instant",
|
| 166 |
+
label="Target Language Model"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
gr.Markdown("π‘ **Sample Queries to try:**\n- *Show me all tasks where priority is High*\n- *List tasks owned by M. Reddy*\n- *Count the number of tasks in each phase*")
|
| 170 |
|
| 171 |
with gr.Column(scale=2):
|
|
|
|
| 178 |
|
| 179 |
submit_btn.click(
|
| 180 |
fn=execute_ai_query,
|
| 181 |
+
inputs=[provider_select, token_input, model_select, query_input],
|
| 182 |
outputs=[output_table, sql_status]
|
| 183 |
)
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
+
demo.launch()
|