jaothan commited on
Commit
df96495
·
verified ·
1 Parent(s): 9511001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -153
app.py CHANGED
@@ -1,153 +1,155 @@
1
- import gradio as gr
2
- import os
3
- import re
4
-
5
- from dotenv import load_dotenv
6
- from contextlib import redirect_stdout
7
- from io import StringIO
8
-
9
-
10
- from langchain import SQLDatabase, SQLDatabaseChain
11
- from langchain.llms import AzureOpenAI
12
- from langchain.agents import create_sql_agent
13
- from langchain.agents.agent_toolkits import SQLDatabaseToolkit
14
- from langchain.agents.agent_types import AgentType
15
-
16
-
17
- load_dotenv(os.getcwd() + "/.env")
18
-
19
- llm = AzureOpenAI(
20
- model_name=os.environ["OPENAI_MODEL_NAME"],
21
- deployment_name=os.environ["OPENAI_DEPLOYMENT_NAME"],
22
- temperature=0,
23
- )
24
-
25
- sqlite_db_path = "data/Chinook.db"
26
- db = SQLDatabase.from_uri(f"sqlite:///{sqlite_db_path}")
27
- db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
28
-
29
- agent_executor = create_sql_agent(
30
- llm=llm,
31
- toolkit=SQLDatabaseToolkit(db=db, llm=llm),
32
- verbose=True,
33
- agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
34
- )
35
-
36
-
37
- def clear_input():
38
- return "", "Hit 'Submit' to see output here"
39
-
40
-
41
- def generate_output_of_db_chain(user_message):
42
- print(user_message)
43
- if not user_message:
44
- print("Empty input")
45
- yield "Please enter a messager before hitting Send!"
46
-
47
- with redirect_stdout(StringIO()) as f:
48
- db_chain.run(user_message)
49
-
50
- s = f.getvalue()
51
- #[6:]: skip first two \n and special tag from LangChain
52
- s = s[6:].replace('\n', '<br/>')
53
-
54
- yield re.sub(r"(\x1b)?\[(\d+[m;])+", "", s)
55
-
56
-
57
- def generate_output_of_db_agent(user_message):
58
- if not user_message:
59
- print("Empty input")
60
- yield "Please enter a messager before hitting Send!"
61
- return ""
62
-
63
- with redirect_stdout(StringIO()) as f:
64
- agent_executor.run(user_message)
65
-
66
- s = f.getvalue()
67
- #[6:]: skip first two \n and special tag from LangChain
68
- s = s[6:].replace("\n", "<br/>")
69
-
70
- yield re.sub(r"(\x1b)?\[(\d+[m;])+", "", s)
71
-
72
-
73
- custom_css = """
74
- #banner-image {
75
- display: block;
76
- margin-left: auto;
77
- margin-right: auto;
78
- }
79
- #chat-message {
80
- font-size: 14px;
81
- min-height: 300px;
82
- }
83
- """
84
-
85
-
86
- with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
87
- gr.HTML("""<h1 align="center">LLM Mini-Series #4 💬</h1>""")
88
-
89
- with gr.Row():
90
- with gr.Column():
91
- gr.Markdown(
92
- f"""
93
- 💻 TODO Add some nice description text
94
- """
95
- )
96
-
97
- # normal SQL Chain
98
- gr.HTML("""<h2 align="left">Using LangChain's SQLDatabaseChain</h2>""")
99
- with gr.Row():
100
- with gr.Column():
101
- user_message = gr.Textbox(
102
- placeholder="Enter your message here",
103
- show_label=False,
104
- elem_id="q-input",
105
- )
106
- with gr.Row():
107
- clear_btn = gr.Button("Clear", elem_id="clear-btn", visible=True)
108
- submit_btn = gr.Button("Submit", elem_id="submit-btn", visible=True)
109
-
110
- with gr.Box():
111
- output_field = gr.HTML(
112
- value="Hit 'Submit' to see output here",
113
- label="Output of model",
114
- interactive=False,
115
- )
116
-
117
- # Agent-based approach
118
- gr.HTML("""<h2 align="left">Using an agent-based approach with LangChain""")
119
- with gr.Row():
120
- with gr.Column():
121
- user_message_agent = gr.Textbox(
122
- placeholder="Enter your message here",
123
- show_label=False,
124
- elem_id="q-agent-input",
125
- )
126
- with gr.Row():
127
- clear_agent_btn = gr.Button(
128
- "Clear", elem_id="clear-agent-btn", visible=True
129
- )
130
- submit_agent_btn = gr.Button(
131
- "Submit", elem_id="submit-agent-btn", visible=True
132
- )
133
-
134
- with gr.Box():
135
- output_agent_field = gr.HTML(
136
- value="Hit 'Submit' to see output here",
137
- label="Output of model",
138
- interactive=False,
139
- )
140
-
141
- clear_btn.click(clear_input, outputs=[user_message, output_field])
142
- submit_btn.click(
143
- generate_output_of_db_chain, inputs=[user_message], outputs=[output_field]
144
- )
145
-
146
- submit_agent_btn.click(
147
- generate_output_of_db_agent,
148
- inputs=[user_message_agent],
149
- outputs=[output_agent_field],
150
- )
151
- clear_agent_btn.click(clear_input, outputs=[user_message_agent, output_agent_field])
152
-
153
- demo.queue(concurrency_count=16).launch(debug=True) # , server_port=8080)
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import re
4
+
5
+ from dotenv import load_dotenv
6
+ from contextlib import redirect_stdout
7
+ from io import StringIO
8
+
9
+
10
+ from langchain import SQLDatabase, SQLDatabaseChain
11
+ from langchain.llms import AzureOpenAI
12
+ from langchain.agents import create_sql_agent
13
+ from langchain.agents.agent_toolkits import SQLDatabaseToolkit
14
+ from langchain.agents.agent_types import AgentType
15
+
16
+ #https://www.youtube.com/watch?v=IN6Q5AwHyLc
17
+
18
+
19
+ load_dotenv(os.getcwd() + "/.env")
20
+
21
+ llm = AzureOpenAI(
22
+ model_name=os.environ["OPENAI_MODEL_NAME"],
23
+ deployment_name=os.environ["OPENAI_DEPLOYMENT_NAME"],
24
+ temperature=0,
25
+ )
26
+
27
+ sqlite_db_path = "data/Chinook.db"
28
+ db = SQLDatabase.from_uri(f"sqlite:///{sqlite_db_path}")
29
+ db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
30
+
31
+ agent_executor = create_sql_agent(
32
+ llm=llm,
33
+ toolkit=SQLDatabaseToolkit(db=db, llm=llm),
34
+ verbose=True,
35
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
36
+ )
37
+
38
+
39
+ def clear_input():
40
+ return "", "Hit 'Submit' to see output here"
41
+
42
+
43
+ def generate_output_of_db_chain(user_message):
44
+ print(user_message)
45
+ if not user_message:
46
+ print("Empty input")
47
+ yield "Please enter a messager before hitting Send!"
48
+
49
+ with redirect_stdout(StringIO()) as f:
50
+ db_chain.run(user_message)
51
+
52
+ s = f.getvalue()
53
+ #[6:]: skip first two \n and special tag from LangChain
54
+ s = s[6:].replace('\n', '<br/>')
55
+
56
+ yield re.sub(r"(\x1b)?\[(\d+[m;])+", "", s)
57
+
58
+
59
+ def generate_output_of_db_agent(user_message):
60
+ if not user_message:
61
+ print("Empty input")
62
+ yield "Please enter a messager before hitting Send!"
63
+ return ""
64
+
65
+ with redirect_stdout(StringIO()) as f:
66
+ agent_executor.run(user_message)
67
+
68
+ s = f.getvalue()
69
+ #[6:]: skip first two \n and special tag from LangChain
70
+ s = s[6:].replace("\n", "<br/>")
71
+
72
+ yield re.sub(r"(\x1b)?\[(\d+[m;])+", "", s)
73
+
74
+
75
+ custom_css = """
76
+ #banner-image {
77
+ display: block;
78
+ margin-left: auto;
79
+ margin-right: auto;
80
+ }
81
+ #chat-message {
82
+ font-size: 14px;
83
+ min-height: 300px;
84
+ }
85
+ """
86
+
87
+
88
+ with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
89
+ gr.HTML("""<h1 align="center">LLM Mini-Series #4 💬</h1>""")
90
+
91
+ with gr.Row():
92
+ with gr.Column():
93
+ gr.Markdown(
94
+ f"""
95
+ 💻 TODO Add some nice description text
96
+ """
97
+ )
98
+
99
+ # normal SQL Chain
100
+ gr.HTML("""<h2 align="left">Using LangChain's SQLDatabaseChain</h2>""")
101
+ with gr.Row():
102
+ with gr.Column():
103
+ user_message = gr.Textbox(
104
+ placeholder="Enter your message here",
105
+ show_label=False,
106
+ elem_id="q-input",
107
+ )
108
+ with gr.Row():
109
+ clear_btn = gr.Button("Clear", elem_id="clear-btn", visible=True)
110
+ submit_btn = gr.Button("Submit", elem_id="submit-btn", visible=True)
111
+
112
+ with gr.Box():
113
+ output_field = gr.HTML(
114
+ value="Hit 'Submit' to see output here",
115
+ label="Output of model",
116
+ interactive=False,
117
+ )
118
+
119
+ # Agent-based approach
120
+ gr.HTML("""<h2 align="left">Using an agent-based approach with LangChain""")
121
+ with gr.Row():
122
+ with gr.Column():
123
+ user_message_agent = gr.Textbox(
124
+ placeholder="Enter your message here",
125
+ show_label=False,
126
+ elem_id="q-agent-input",
127
+ )
128
+ with gr.Row():
129
+ clear_agent_btn = gr.Button(
130
+ "Clear", elem_id="clear-agent-btn", visible=True
131
+ )
132
+ submit_agent_btn = gr.Button(
133
+ "Submit", elem_id="submit-agent-btn", visible=True
134
+ )
135
+
136
+ with gr.Box():
137
+ output_agent_field = gr.HTML(
138
+ value="Hit 'Submit' to see output here",
139
+ label="Output of model",
140
+ interactive=False,
141
+ )
142
+
143
+ clear_btn.click(clear_input, outputs=[user_message, output_field])
144
+ submit_btn.click(
145
+ generate_output_of_db_chain, inputs=[user_message], outputs=[output_field]
146
+ )
147
+
148
+ submit_agent_btn.click(
149
+ generate_output_of_db_agent,
150
+ inputs=[user_message_agent],
151
+ outputs=[output_agent_field],
152
+ )
153
+ clear_agent_btn.click(clear_input, outputs=[user_message_agent, output_agent_field])
154
+
155
+ demo.queue(concurrency_count=16).launch(debug=True) # , server_port=8080)