Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import json
|
4 |
+
import plotly.express as px
|
5 |
+
import re
|
6 |
+
import io
|
7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
+
import torch
|
9 |
+
|
10 |
+
# Load DeepSeek Model
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
|
13 |
+
|
14 |
+
def query_deepseek(prompt):
|
15 |
+
"""
|
16 |
+
Query the DeepSeek model and return the response.
|
17 |
+
"""
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(**inputs, max_new_tokens=150)
|
21 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
return response.strip()
|
23 |
+
|
24 |
+
def extract_json(text):
|
25 |
+
"""
|
26 |
+
Extract JSON from the DeepSeek response using regex.
|
27 |
+
"""
|
28 |
+
match = re.search(r"\{.*\}", text, re.DOTALL)
|
29 |
+
if match:
|
30 |
+
try:
|
31 |
+
return json.loads(match.group(0))
|
32 |
+
except json.JSONDecodeError:
|
33 |
+
st.error("JSON Decode Error!")
|
34 |
+
return None
|
35 |
+
return None
|
36 |
+
|
37 |
+
def get_visualization_suggestion(data):
|
38 |
+
"""
|
39 |
+
Send dataset columns to DeepSeek and get suggestions for visualization.
|
40 |
+
"""
|
41 |
+
prompt = f"""
|
42 |
+
I have the following dataset columns: {', '.join(data.columns)}.
|
43 |
+
Suggest the best type of visualization for this dataset.
|
44 |
+
Return only a valid JSON response in the following format:
|
45 |
+
{{
|
46 |
+
"x": "column_name",
|
47 |
+
"y": "column_name",
|
48 |
+
"chart_type": "bar/line/scatter/pie"
|
49 |
+
}}
|
50 |
+
"""
|
51 |
+
response = query_deepseek(prompt)
|
52 |
+
return extract_json(response)
|
53 |
+
|
54 |
+
def extract_csv_from_response(response):
|
55 |
+
"""
|
56 |
+
Dynamically extract CSV data from a response string.
|
57 |
+
"""
|
58 |
+
lines = response.splitlines()
|
59 |
+
csv_data = [line.strip() for line in lines if '"' in line and ',' in line]
|
60 |
+
return '\n'.join(csv_data) if csv_data else None
|
61 |
+
|
62 |
+
def generate_demo_data_csv(user_input, num_rows=10):
|
63 |
+
"""Generates realistic demo data using the LLM in valid CSV format."""
|
64 |
+
prompt = f"""
|
65 |
+
Generate a structured dataset with {num_rows} rows based on the following request:
|
66 |
+
"{user_input}"
|
67 |
+
Ensure the response is in valid CSV format, with column headers and quoted text values.
|
68 |
+
"""
|
69 |
+
response = query_deepseek(prompt).strip()
|
70 |
+
csv_data = extract_csv_from_response(response)
|
71 |
+
|
72 |
+
if csv_data:
|
73 |
+
try:
|
74 |
+
df = pd.read_csv(io.StringIO(csv_data))
|
75 |
+
file_path = "generated_data.csv"
|
76 |
+
df.to_csv(file_path, index=False)
|
77 |
+
return "Demo data generated as CSV.", file_path
|
78 |
+
except Exception as e:
|
79 |
+
return f"Error: Invalid CSV format. {str(e)}", None
|
80 |
+
else:
|
81 |
+
return "Error: No valid CSV data found in the response.", None
|
82 |
+
|
83 |
+
def query_sql_generator(user_query):
|
84 |
+
"""Generate SQL queries from natural language."""
|
85 |
+
prompt = f"I just want a SQL Query corresponding to: {user_query} and no explanation."
|
86 |
+
return query_deepseek(prompt)
|
87 |
+
|
88 |
+
# Streamlit UI
|
89 |
+
st.set_page_config(page_title="AI-Powered Dashboard", layout="wide")
|
90 |
+
st.title("π€ AI-Powered Multi-Feature Dashboard")
|
91 |
+
|
92 |
+
# Sidebar for navigation
|
93 |
+
st.sidebar.title("Navigation")
|
94 |
+
option = st.sidebar.radio("Select Feature", ["π Data Visualization", "π§ SQL Query Generator", "π Demo Data Generator"])
|
95 |
+
|
96 |
+
if option == "π Data Visualization":
|
97 |
+
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
98 |
+
if uploaded_file is not None:
|
99 |
+
df = pd.read_csv(uploaded_file)
|
100 |
+
st.write("### Preview of Data")
|
101 |
+
st.dataframe(df.head())
|
102 |
+
|
103 |
+
with st.spinner("Getting visualization suggestions from DeepSeek..."):
|
104 |
+
suggestion = get_visualization_suggestion(df)
|
105 |
+
|
106 |
+
if suggestion:
|
107 |
+
chart_type, x_col, y_col = suggestion.get("chart_type"), suggestion.get("x"), suggestion.get("y")
|
108 |
+
|
109 |
+
if x_col not in df.columns or y_col not in df.columns:
|
110 |
+
st.error("DeepSeek suggested invalid column names.")
|
111 |
+
else:
|
112 |
+
st.write(f"### Suggested Chart: {chart_type.capitalize()} Chart")
|
113 |
+
chart_map = {
|
114 |
+
"bar": px.bar,
|
115 |
+
"line": px.line,
|
116 |
+
"scatter": px.scatter,
|
117 |
+
"pie": lambda df, x, y: px.pie(df, names=x, values=y)
|
118 |
+
}
|
119 |
+
if chart_type in chart_map:
|
120 |
+
fig = chart_map[chart_type](df, x=x_col, y=y_col, title=f"{x_col} vs {y_col}")
|
121 |
+
st.plotly_chart(fig)
|
122 |
+
else:
|
123 |
+
st.error("Unsupported chart type suggested.")
|
124 |
+
|
125 |
+
elif option == "π§ SQL Query Generator":
|
126 |
+
text_input = st.text_area("Enter your Query here in Plain English:")
|
127 |
+
if st.button("Generate SQL Query"):
|
128 |
+
with st.spinner("Generating SQL Query..."):
|
129 |
+
st.write(query_sql_generator(text_input))
|
130 |
+
|
131 |
+
elif option == "π Demo Data Generator":
|
132 |
+
user_input = st.text_area("Describe the dataset you want:")
|
133 |
+
num_rows = st.number_input("Number of rows", min_value=1, max_value=1000, value=10)
|
134 |
+
if st.button("Generate Dataset"):
|
135 |
+
with st.spinner("Generating Demo Data..."):
|
136 |
+
message, file_path = generate_demo_data_csv(user_input, num_rows)
|
137 |
+
st.write(message)
|
138 |
+
if file_path:
|
139 |
+
st.download_button("Download CSV", open(file_path, "rb"), file_name="generated_data.csv", mime="text/csv")
|