feat: new gradio UI
Browse files- gradio_app.py +86 -41
- langchain_mcp_client.py +1 -1
- requirements.txt +0 -0
- resources/pialogo.png +0 -0
gradio_app.py
CHANGED
|
@@ -29,48 +29,94 @@ def image_to_base64_markdown(image_path, alt_text="Customer Status"):
|
|
| 29 |
|
| 30 |
# ====================================== Async-compatible wrapper
|
| 31 |
async def run_agent(request, history):
|
| 32 |
-
# configs = load_db_configs()
|
| 33 |
-
# final_answer, last_tool_answer, = await pg_mcp_exec(request)
|
| 34 |
-
# return final_answer, last_tool_answer
|
| 35 |
-
|
| 36 |
response, messages = await lc_mcp_exec(request, history)
|
| 37 |
|
| 38 |
image_path = ""
|
| 39 |
load_dotenv()
|
| 40 |
-
PANDAS_EXPORTS_PATH = os.getenv("PANDAS_EXPORTS_PATH")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
generated_files = [f for f in os.listdir(PANDAS_EXPORTS_PATH) if f.startswith("temp_chart_") and f.endswith(".png")]
|
| 42 |
if generated_files:
|
| 43 |
image_path = os.path.join(PANDAS_EXPORTS_PATH, generated_files[0])
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
-
# image_path = f"{PANDAS_EXPORTS_PATH}/blank_chart.png"
|
| 50 |
-
# image_markdown = image_to_base64_markdown(image_path)
|
| 51 |
output = response
|
| 52 |
|
| 53 |
print(f"Image path: {image_path}")
|
| 54 |
-
|
| 55 |
-
# print(output)
|
| 56 |
|
| 57 |
return output
|
| 58 |
|
| 59 |
|
| 60 |
# ====================================== Gradio UI with history
|
| 61 |
-
LOGO_PATH = "resources/
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
fn=run_agent,
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
examples=[
|
|
|
|
| 73 |
"List all tables in the database",
|
|
|
|
| 74 |
"how many customers do you have?",
|
| 75 |
"what are the statuses my of my customers",
|
| 76 |
"Visualize with different colors and show legend",
|
|
@@ -80,30 +126,29 @@ with gr.Blocks() as demo:
|
|
| 80 |
"How many users and roles have been created in 2024"
|
| 81 |
],
|
| 82 |
save_history=True,
|
|
|
|
| 83 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
-
# Gradio UI
|
| 87 |
-
# demo = gr.Interface(
|
| 88 |
-
# fn=run_agent,
|
| 89 |
-
# inputs=gr.Textbox(
|
| 90 |
-
# label="Natural Language Request",
|
| 91 |
-
# placeholder="e.g., Show me the table of join posts and users tables."
|
| 92 |
-
# ),
|
| 93 |
-
# outputs=gr.Markdown(
|
| 94 |
-
# label="SQL Query / Result",
|
| 95 |
-
# value="# Result \n \n",
|
| 96 |
-
# # max_height=1500,
|
| 97 |
-
# height= 1500
|
| 98 |
-
# ),
|
| 99 |
-
# title="PostgreSQL Query Agent",
|
| 100 |
-
# description="Ask your database in natural language and get results.",
|
| 101 |
-
# flagging_mode="never"
|
| 102 |
-
# )
|
| 103 |
|
| 104 |
|
| 105 |
# TODO: maybe we can add a mcp tool to validate the results (those converted to DataFrame) to make sure the valid type is passed to the visualization tool by ReAct agent
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# ====================================== Async-compatible wrapper
|
| 31 |
async def run_agent(request, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
response, messages = await lc_mcp_exec(request, history)
|
| 33 |
|
| 34 |
image_path = ""
|
| 35 |
load_dotenv()
|
| 36 |
+
PANDAS_EXPORTS_PATH = os.getenv("PANDAS_EXPORTS_PATH", "exports/charts")
|
| 37 |
+
|
| 38 |
+
# Ensure the exports directory exists
|
| 39 |
+
os.makedirs(PANDAS_EXPORTS_PATH, exist_ok=True)
|
| 40 |
+
|
| 41 |
generated_files = [f for f in os.listdir(PANDAS_EXPORTS_PATH) if f.startswith("temp_chart_") and f.endswith(".png")]
|
| 42 |
if generated_files:
|
| 43 |
image_path = os.path.join(PANDAS_EXPORTS_PATH, generated_files[0])
|
| 44 |
+
try:
|
| 45 |
+
image_markdown = image_to_base64_markdown(image_path)
|
| 46 |
+
output = f"{image_markdown}\n\n{response}"
|
| 47 |
+
# Remove the image file after reading it
|
| 48 |
+
os.remove(image_path)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error processing image: {e}")
|
| 51 |
+
output = response
|
| 52 |
else:
|
|
|
|
|
|
|
| 53 |
output = response
|
| 54 |
|
| 55 |
print(f"Image path: {image_path}")
|
| 56 |
+
print(f"Output length: {len(output)}")
|
|
|
|
| 57 |
|
| 58 |
return output
|
| 59 |
|
| 60 |
|
| 61 |
# ====================================== Gradio UI with history
|
| 62 |
+
LOGO_PATH = "resources/pialogo.png"
|
| 63 |
+
|
| 64 |
+
# CSS customizations
|
| 65 |
+
custom_css = """
|
| 66 |
+
.container {
|
| 67 |
+
max-width: 2200px !important;
|
| 68 |
+
margin: auto;
|
| 69 |
+
padding: 20px;
|
| 70 |
+
}
|
| 71 |
+
.chat-container {
|
| 72 |
+
height: 1000px !important;
|
| 73 |
+
min-height: 1000px !important;
|
| 74 |
+
overflow-y: auto;
|
| 75 |
+
}
|
| 76 |
+
.message-container {
|
| 77 |
+
padding: 15px;
|
| 78 |
+
border-radius: 10px;
|
| 79 |
+
margin: 10px 0;
|
| 80 |
+
}
|
| 81 |
+
.markdown-content {
|
| 82 |
+
font-size: 18px;
|
| 83 |
+
line-height: 1.7;
|
| 84 |
+
}
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 88 |
+
with gr.Row(elem_classes="container"):
|
| 89 |
+
# with gr.Column(scale=1):
|
| 90 |
+
# gr.Image(value=LOGO_PATH, height=200, show_label=False)
|
| 91 |
+
|
| 92 |
+
with gr.Column(scale=3):
|
| 93 |
+
gr.Markdown(
|
| 94 |
+
"""
|
| 95 |
+
<h1 style='text-align: center; margin-bottom: 1rem'>Talk to Your Data</h1>
|
| 96 |
+
<p style='text-align: center'>Ask questions about your database, analyze and visualize data.</p>
|
| 97 |
+
"""
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Row(elem_classes="container"):
|
| 101 |
+
with gr.Column(scale=3):
|
| 102 |
+
chat = gr.ChatInterface(
|
| 103 |
fn=run_agent,
|
| 104 |
+
chatbot=gr.Chatbot(
|
| 105 |
+
height=1000,
|
| 106 |
+
show_label=False,
|
| 107 |
+
elem_classes="chat-container",
|
| 108 |
+
render_markdown=True
|
| 109 |
+
),
|
| 110 |
+
textbox=gr.Textbox(
|
| 111 |
+
placeholder="Type your questions here...",
|
| 112 |
+
container=False,
|
| 113 |
+
scale=4
|
| 114 |
+
),
|
| 115 |
+
theme="soft",
|
| 116 |
examples=[
|
| 117 |
+
"Describe the database",
|
| 118 |
"List all tables in the database",
|
| 119 |
+
"List all tables with columns and data types",
|
| 120 |
"how many customers do you have?",
|
| 121 |
"what are the statuses my of my customers",
|
| 122 |
"Visualize with different colors and show legend",
|
|
|
|
| 126 |
"How many users and roles have been created in 2024"
|
| 127 |
],
|
| 128 |
save_history=True,
|
| 129 |
+
type="messages"
|
| 130 |
)
|
| 131 |
+
|
| 132 |
+
with gr.Column(scale=1):
|
| 133 |
+
with gr.Accordion("Example Questions", open=True):
|
| 134 |
+
gr.Markdown("""
|
| 135 |
+
- 📊 List all tables in database
|
| 136 |
+
- 👥 Total number of customers
|
| 137 |
+
- 📈 Visualize it with different colors
|
| 138 |
+
- 📋 Order statistics for last 6 years
|
| 139 |
+
- 📆 User and role counts in 2024
|
| 140 |
+
""")
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
# TODO: maybe we can add a mcp tool to validate the results (those converted to DataFrame) to make sure the valid type is passed to the visualization tool by ReAct agent
|
| 146 |
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
+
demo.launch(
|
| 150 |
+
server_name="0.0.0.0",
|
| 151 |
+
server_port=7860,
|
| 152 |
+
share=True,
|
| 153 |
+
debug=True
|
| 154 |
+
)
|
langchain_mcp_client.py
CHANGED
|
@@ -12,7 +12,7 @@ import logging
|
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
from langchain.globals import set_debug
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
|
| 18 |
# Set up logging
|
|
|
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
from langchain.globals import set_debug
|
| 14 |
|
| 15 |
+
set_debug(True)
|
| 16 |
|
| 17 |
|
| 18 |
# Set up logging
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
resources/pialogo.png
ADDED
|