Upload 4 files
Browse files- .gitattributes +2 -0
- README.md +10 -1
- app.py +153 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/San_Francisco_Trees.db filter=lfs diff=lfs merge=lfs -text
|
37 |
+
data/Street_Tree_List.csv filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1 |
---
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Llm Mini Series 4
|
3 |
+
emoji: ๐
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.44.3
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.35.2
|
2 |
+
langchain==0.0.205
|
3 |
+
openai==0.27.6
|
4 |
+
python-dotenv==1.0.0
|