File size: 14,152 Bytes
22a068b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf4f395
22a068b
 
 
 
 
 
 
 
67d7a17
 
22a068b
 
bf4f395
 
 
 
 
 
 
 
 
 
22a068b
 
bd3809b
22a068b
bd3809b
22a068b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67d7a17
 
22a068b
67d7a17
22a068b
 
 
 
 
 
 
 
 
 
bf4f395
22a068b
bf4f395
22a068b
bf4f395
 
 
 
22a068b
 
 
 
 
 
 
 
 
 
bd3809b
 
22a068b
 
 
 
 
 
 
 
 
bd3809b
 
bf4f395
22a068b
bd3809b
 
 
 
 
 
 
bf4f395
bd3809b
 
22a068b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd3809b
22a068b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf4f395
22a068b
bf4f395
22a068b
 
 
bd3809b
22a068b
 
 
bd3809b
22a068b
 
 
 
 
 
 
 
 
bf4f395
22a068b
 
 
 
 
bf4f395
 
22a068b
bf4f395
 
 
 
22a068b
 
 
 
bf4f395
22a068b
 
bd3809b
22a068b
 
 
 
 
d1808bb
22a068b
bd3809b
 
d1808bb
 
bd3809b
 
 
d1808bb
 
22a068b
bd3809b
22a068b
bd3809b
 
d1808bb
 
bd3809b
 
 
d1808bb
 
22a068b
 
bf4f395
22a068b
 
bd3809b
22a068b
 
 
 
 
 
 
 
d1808bb
 
22a068b
 
d1808bb
22a068b
bf4f395
22a068b
d1808bb
 
22a068b
bf4f395
 
 
 
d1808bb
 
bf4f395
22a068b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# --- PYTHON 3.13 COMPATIBILITY MONKEY-PATCH ---
import sys
import types

if 'audioop' not in sys.modules:
    mock_audioop = types.ModuleType('audioop')
    mock_audioop.error = Exception
    mock_audioop.getsample = lambda data, width, index: 0 
    sys.modules['audioop'] = mock_audioop
# ----------------------------------------------

import os
import sqlite3
import pandas as pd
import json
import gradio as gr
from groq import Groq
import requests

# --- Database Initialization ---
DB_NAME = "strides_production.db"

def init_db():
    conn = sqlite3.connect(DB_NAME)
    cursor = conn.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS project_tasks (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
            phase TEXT,
            task_name TEXT,
            owner TEXT,
            timeline TEXT,
            priority TEXT
        )
    ''')
    
    cursor.execute("SELECT COUNT(*) FROM project_tasks")
    if cursor.fetchone()[0] == 0:
        dummy_data = [
            ('Discovery', 'Identify Deviation Root Cause', 'A. Sharma', 'Week 1', 'High'),
            ('Data Prep', 'Clean LIMS Batch Records', 'R. Verma', 'Week 2-3', 'Medium'),
            ('Modeling', 'Train Predictive Quality AI', 'S. Iyer', 'Week 4-5', 'High'),
            ('Validation', 'Draft GxP Validation Protocol', 'M. Reddy', 'Week 6', 'High'),
            ('Discovery', 'Audit Existing Logbook Manuals', 'P. Gupta', 'Week 1', 'Low'),
            ('Data Prep', 'Standardize Sensor Timestamp Data', 'R. Verma', 'Week 2-3', 'Medium'),
            ('Validation', 'Perform IQ/OQ/PQ for AI Model', 'M. Reddy', 'Week 6', 'High')
        ]
        cursor.executemany(
            'INSERT INTO project_tasks (phase, task_name, owner, timeline, priority) VALUES (?,?,?,?,?)', 
            dummy_data
        )
    conn.commit()
    conn.close()

init_db()

# --- Model Selection Repositories ---
GROQ_MODELS = [
    "llama-3.1-8b-instant", 
    "llama-3.1-70b-versatile", 
    "openai/gpt-oss-20b", 
    "whisper-large-v3-turbo"
]

OPENROUTER_MODELS = [
    "nvidia/nemotron-3-ultra-550b-a55b:free",
    "nvidia/nemotron-3.5-content-safety:free"
]

# --- Reactive State Event Handlers ---
def change_provider_options(provider_choice):
    if provider_choice == "Groq":
        return gr.Dropdown(choices=GROQ_MODELS, value=GROQ_MODELS[0], interactive=True)
    elif provider_choice == "OpenRouter":
        return gr.Dropdown(choices=OPENROUTER_MODELS, value=OPENROUTER_MODELS[0], interactive=True)
    return gr.Dropdown(choices=GROQ_MODELS, value=GROQ_MODELS[0], interactive=True)

def clear_chatbot_history():
    return [], ""

# --- Common LLM API Request Orchestrator ---
def call_llm(provider, api_key, model_choice, system_prompt, history_messages):
    messages = [{"role": "system", "content": system_prompt}]
    messages.extend(history_messages)

    if provider == "Groq":
        client = Groq(api_key=api_key)
        groq_model_map = {
            "llama-3.1-8b-instant": "llama-3.1-8b-instant",
            "llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
            "openai/gpt-oss-20b": "llama-3.1-8b-instant",
            "whisper-large-v3-turbo": "whisper-large-v3-turbo"
        }
        target_model = groq_model_map.get(model_choice, "llama-3.1-8b-instant")
        response = client.chat.completions.create(
            model=target_model, messages=messages, temperature=0.0
        )
        return response.choices[0].message.content.strip()

    elif provider == "OpenRouter":
        headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
        openrouter_model_map = {
            "nvidia/nemotron-3-ultra-550b-a55b:free": "nvidia/nemotron-3-ultra-550b-a55b:free",
            "nvidia/nemotron-3.5-content-safety:free": "nvidia/nemotron-3.5-content-safety:free"
        }
        target_model = openrouter_model_map.get(model_choice, "nvidia/nemotron-3-ultra-550b-a55b:free")
        
        payload = {
            "model": target_model, 
            "messages": messages, 
            "temperature": 0.0
        }
        response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
        if response.status_code == 200:
            return response.json()['choices'][0]['message']['content'].strip()
        else:
            raise Exception(f"OpenRouter Portal Error ({response.status_code}): {response.text}")

# --- Tab 1: Text-to-SQL Desk Core Logic ---
def execute_ai_query(provider, api_key, model_choice, user_question):
    if not api_key or not api_key.strip():
        return None, "⚠️ Authentication missing. Please input your Secret Key on the left menu pane."
    if not user_question or not user_question.strip():
        return None, "⚠️ Query field is blank. Please enter a request."

    system_prompt = (
        "You are an expert SQL assistant. The database table is called 'project_tasks' with columns: "
        "[id, timestamp, phase, task_name, owner, timeline, priority]. "
        "Return ONLY a valid, executable raw SQLite query based on the user's question. "
        "Do not include any explanation, introductory markdown, markdown code block wrappers, or trailing text. "
        "Output raw text only."
    )

    try:
        query_as_history = [{"role": "user", "content": user_question}]
        sql_query = call_llm(provider, api_key, model_choice, system_prompt, query_as_history)
        sql_query = sql_query.replace("```sql", "").replace("```", "").replace("`", "").strip()

        conn = sqlite3.connect(DB_NAME)
        df = pd.read_sql_query(sql_query, conn)
        conn.close()
        return df, f"⚑ **Generated SQL Query:** `{sql_query}`"
    except Exception as e:
        return None, f"❌ Execution Failed: {str(e)}"

# --- Tab 2: Decoupled Multi-Step Chatbot Logic ---
def append_user_message(chat_history, user_msg):
    if not user_msg or not user_msg.strip():
        return chat_history, ""
    chat_history.append({"role": "user", "content": user_msg})
    return chat_history, ""

def generate_agent_response(chat_history, provider, api_key, model_choice):
    if not chat_history:
        return chat_history
    
    if not api_key or not api_key.strip():
        chat_history.append({"role": "assistant", "content": "⚠️ Authentication missing. Please input your API Key on the left menu pane."})
        return chat_history

    system_prompt = (
        "You are a helpful conversational assistant and workflow coordinator for the Strides Pharma AI operational framework.\n"
        "Your goal is twofold:\n"
        "1. Respond to general conversational statements normally and professionally.\n"
        "2. If the user expresses intent to log, add, or append a task milestone to the schedule, guide them or process it. "
        "The required metrics are: Phase, Task_Name, Owner, Timeline, and Priority.\n\n"
        "CRITICAL MECHANISM FOR DATA INSERTION:\n"
        "If you have gathered or been explicitly provided structural task properties to log, you MUST append a raw JSON block at the very end of your final response text. "
        "The format must exactly be: ||JSON_DATA: {\"phase\": \"...\", \"task_name\": \"...\", \"owner\": \"...\", \"timeline\": \"...\", \"priority\": \"...\"}||\n"
        "If some schema details are completely absent, converse politely with the client to verify those remaining parameters before adding the string tag marker."
    )

    formatted_history = []
    for turn in chat_history:
        formatted_history.append({"role": turn["role"], "content": turn["content"]})

    try:
        raw_response = call_llm(provider, api_key, model_choice, system_prompt, formatted_history)
        cleaned_response = raw_response
        database_committed_alert = ""
        
        if "||JSON_DATA:" in raw_response:
            try:
                parts = raw_response.split("||JSON_DATA:")
                cleaned_response = parts[0].strip()
                json_string = parts[1].replace("||", "").strip()
                
                data_payload = json.loads(json_string)
                
                conn = sqlite3.connect(DB_NAME)
                cursor = conn.cursor()
                cursor.execute(
                    "INSERT INTO project_tasks (phase, task_name, owner, timeline, priority) VALUES (?, ?, ?, ?, ?)",
                    (data_payload.get('phase'), data_payload.get('task_name'), data_payload.get('owner'), data_payload.get('timeline'), data_payload.get('priority'))
                )
                conn.commit()
                conn.close()
                
                database_committed_alert = f"\n\nβš™οΈ **[SYSTEM UPDATE]:** Successfully appended task '{data_payload.get('task_name')}' to the database master records."
            except Exception as inner_err:
                database_committed_alert = f"\n\n⚠️ **[SYSTEM NOTICE]:** Captured parameters, but update aborted due to structural parsing errors: {str(inner_err)}"

        final_display_text = cleaned_response + database_committed_alert
        chat_history.append({"role": "assistant", "content": final_display_text})
        return chat_history

    except Exception as e:
        chat_history.append({"role": "assistant", "content": f"❌ API Connection Failure: {str(e)}"})
        return chat_history

# --- Interface Layout Configuration ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸ’Š Strides Pharma AI Production & Data Portal")
    gr.Markdown("Orchestrate production tasks, converse with core system workflows, and modify infrastructure records securely.")
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ”‘ Global Control Tower")
            provider_select = gr.Dropdown(choices=["Groq", "OpenRouter"], value="Groq", label="API Gateway Provider", interactive=True)
            token_input = gr.Textbox(label="User API Secret Key", type="password", placeholder="gsk_... or sk-or-...")
            
            model_select = gr.Dropdown(
                choices=GROQ_MODELS, 
                value=GROQ_MODELS[0], 
                label="Target Engine Architecture",
                interactive=True
            )
            
            clear_session_btn = gr.Button("πŸ”„ Reset Current Session State", variant="stop")
            
            gr.Markdown("✨ **Global Platform Database State Schema:**\n- `phase` (Discovery, Data Prep, Modeling, Validation)\n- `task_name` (Structural description string)\n- `owner` (Personnel name)\n- `timeline` (Operational windows)\n- `priority` (High, Medium, Low)")
            
        with gr.Column(scale=2):
            with gr.Tabs():
                
                # Tab 1: Interactive Data Contributor Chatbot Workspace
                with gr.TabItem("πŸ€– Interactive Data Contributor Chatbot"):
                    gr.Markdown("### Conversational Contributor Agent")
                    gr.Markdown("Chat with this engine normally, or log a brand-new task assignment milestone directly into the active SQLite infrastructure.")
                    
                    chatbot_viewport = gr.Chatbot(type="messages", label="Operational History Workspace")
                    chat_input = gr.Textbox(placeholder="Say hello, or submit task details to log...", label="Your Message")
                    send_btn = gr.Button("Submit Message", variant="primary")
                    
                    # Decoupled transaction bindings with explicit api_name parameters disabled
                    send_btn.click(
                        fn=append_user_message,
                        inputs=[chatbot_viewport, chat_input],
                        outputs=[chatbot_viewport, chat_input],
                        api_name=False
                    ).then(
                        fn=generate_agent_response,
                        inputs=[chatbot_viewport, provider_select, token_input, model_select],
                        outputs=[chatbot_viewport],
                        api_name=False
                    )
                    
                    chat_input.submit(
                        fn=append_user_message,
                        inputs=[chatbot_viewport, chat_input],
                        outputs=[chatbot_viewport, chat_input],
                        api_name=False
                    ).then(
                        fn=generate_agent_response,
                        inputs=[chatbot_viewport, provider_select, token_input, model_select],
                        outputs=[chatbot_viewport],
                        api_name=False
                    )
                
                # Tab 2: Natural-Language-to-SQL Inquisitor Desk Node
                with gr.TabItem("πŸ”Ž SQL Inquisitor Desk"):
                    gr.Markdown("### Natural Language SQL Query Engine")
                    query_input = gr.Textbox(label="Query current database contents using conversational English:", placeholder="e.g., Show me all records sorted by priority status")
                    query_btn = gr.Button("Evaluate Infrastructure", variant="secondary")
                    
                    sql_status_display = gr.Markdown()
                    output_data_table = gr.DataFrame(label="Queried Records Dataframe Live Output")
                    
                    query_btn.click(
                        fn=execute_ai_query,
                        inputs=[provider_select, token_input, model_select, query_input],
                        outputs=[output_data_table, sql_status_display],
                        api_name=False
                    )

    # --- REACTIVE STATE BINDINGS (Bypassing API Compilation Loops) ---
    provider_select.change(
        fn=change_provider_options,
        inputs=[provider_select],
        outputs=[model_select],
        api_name=False
    )
    
    clear_session_btn.click(
        fn=clear_chatbot_history,
        inputs=[],
        outputs=[chatbot_viewport, chat_input],
        api_name=False
    )

if __name__ == "__main__":
    demo.launch()