Sarath0x8f commited on
Commit
a3621be
·
verified ·
1 Parent(s): fd17713

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title Gradio UI - 1
2
+ import gradio as gr
3
+ import os
4
+ from langchain.chat_models import init_chat_model
5
+ from langchain_community.utilities import SQLDatabase
6
+ from langchain_community.agent_toolkits import SQLDatabaseToolkit
7
+ from langchain.agents import create_agent
8
+ from google.colab import userdata
9
+
10
+ # --- GLOBALS ---
11
+ model = None # ✅ prevent "NameError" before setup
12
+ db = None
13
+ toolkit = None
14
+ agent = None
15
+
16
+ # Define available models for each provider
17
+ PROVIDER_MODELS = {
18
+ "google_genai": ["gemini-2.0-pro", "gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.0-flash-thinking"],
19
+ "openai": ["gpt-4o", "gpt-4o-mini", "gpt-3.5-turbo"],
20
+ "anthropic": ["claude-3-opus", "claude-3-sonnet", "claude-3-haiku"],
21
+ "azure_openai": ["gpt-4-turbo", "gpt-4o-mini", "gpt-35-turbo"],
22
+ "bedrock": ["anthropic.claude-3-sonnet-v1", "mistral.mixtral-8x7b"],
23
+ "xai": ["grok-2", "grok-2-mini"],
24
+ "deepseek": ["deepseek-chat", "deepseek-coder"],
25
+ "perplexity": ["sonar-small-chat", "sonar-medium-chat", "sonar-large-chat"]
26
+ }
27
+
28
+
29
+ def create_chatbot_interface():
30
+ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Noto Sans")]),
31
+ css='footer {visibility: hidden}') as demo:
32
+ with gr.Row():
33
+ with gr.Column(scale=1):
34
+ gr.Markdown("## LLM Setup")
35
+ llm_provider = gr.Dropdown(
36
+ list(PROVIDER_MODELS.keys()), label="Select Provider", value="google_genai"
37
+ )
38
+ llm_model = gr.Dropdown(
39
+ choices=PROVIDER_MODELS["google_genai"],
40
+ label="Select Model",
41
+ value="gemini-2.5-flash-lite"
42
+ )
43
+ api_key = gr.Textbox(label="Enter API Key", type="password")
44
+ setup_llm_btn = gr.Button("Setup LLM")
45
+
46
+ gr.Markdown("## Database Connection")
47
+ db_connection_string = gr.Textbox(label="Enter Connection String", type="password")
48
+ connect_db_btn = gr.Button("Connect to Database")
49
+ db_status = gr.Markdown("") # ✅ show DB connection feedback
50
+
51
+ with gr.Column(scale=2):
52
+ gr.Markdown("## Chatbot Interface")
53
+ chatbot = gr.Chatbot()
54
+ msg = gr.Textbox(label="Enter your question")
55
+ clear = gr.Button("Clear")
56
+
57
+ # --- FUNCTIONS ---
58
+
59
+ def update_model_dropdown(provider):
60
+ models = PROVIDER_MODELS.get(provider, [])
61
+ default = models[0] if models else None
62
+ return gr.update(choices=models, value=default)
63
+
64
+ def setup_llm(provider, model_name, key):
65
+ os.environ["GOOGLE_API_KEY"] = key
66
+ global model
67
+ model = init_chat_model(model_name, model_provider=provider)
68
+ return f"✅ LLM model `{model_name}` from `{provider}` setup successfully."
69
+
70
+ def connect_to_db(db_url):
71
+ global db, toolkit, tools, agent, system_prompt, model
72
+ if model is None:
73
+ return "❌ Please set up the LLM before connecting to the database."
74
+
75
+ db = SQLDatabase.from_uri(db_url)
76
+ toolkit = SQLDatabaseToolkit(db=db, llm=model)
77
+ tools = toolkit.get_tools()
78
+
79
+ system_prompt = """
80
+ You are an agent designed to interact with a SQL database.
81
+ Given an input question, create a syntactically correct {dialect} query to run,
82
+ then look at the results of the query and return the answer. Unless the user
83
+ specifies a specific number of examples they wish to obtain, always limit your
84
+ query to at most {top_k} results.
85
+
86
+ You can order the results by a relevant column to return the most interesting
87
+ examples in the database. Never query for all the columns from a specific table,
88
+ only ask for the relevant columns given the question.
89
+
90
+ You MUST double check your query before executing it. If you get an error while
91
+ executing a query, rewrite the query and try again.
92
+
93
+ DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the
94
+ database.
95
+
96
+ To start you should ALWAYS look at the tables in the database to see what you
97
+ can query. Do NOT skip this step.
98
+
99
+ Then you should query the schema of the most relevant tables.
100
+ """.format(
101
+ dialect=db.dialect,
102
+ top_k=5,
103
+ )
104
+
105
+ agent = create_agent(model, tools, system_prompt=system_prompt)
106
+ tables = db.get_usable_table_names()
107
+ return f"✅ Connected to database successfully.\n\n**Available tables:** {tables}"
108
+
109
+ def respond(message, chat_history):
110
+ result = ""
111
+ for step in agent.stream(
112
+ {"messages": [{"role": "user", "content": message}]},
113
+ stream_mode="values",
114
+ ):
115
+ result += step["messages"][-1].content
116
+ chat_history.append((message, result))
117
+ return "", chat_history
118
+
119
+ # --- EVENTS ---
120
+ llm_provider.change(update_model_dropdown, inputs=llm_provider, outputs=llm_model)
121
+ setup_llm_btn.click(setup_llm, inputs=[llm_provider, llm_model, api_key], outputs=None)
122
+ connect_db_btn.click(connect_to_db, inputs=[db_connection_string], outputs=db_status)
123
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
124
+ clear.click(lambda: None, None, chatbot, queue=False)
125
+
126
+ return demo
127
+
128
+
129
+ if __name__ == "__main__":
130
+ demo = create_chatbot_interface()
131
+ demo.launch(debug=True)