Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,16 @@ import pandas as pd
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from google import genai
|
| 5 |
|
| 6 |
-
# π ADD YOUR GEMINI API KEY
|
| 7 |
client = genai.Client(api_key="AIzaSyCvvewIh6BVyULlEmw1QE75vWsEmu8QNH0")
|
| 8 |
|
| 9 |
|
| 10 |
# ---------- READ FILE ----------
|
| 11 |
def read_file(file):
|
| 12 |
try:
|
|
|
|
|
|
|
|
|
|
| 13 |
if file.name.endswith(".xlsx"):
|
| 14 |
return pd.read_excel(file.name)
|
| 15 |
else:
|
|
@@ -26,10 +29,13 @@ def preview(file):
|
|
| 26 |
return df.head(20)
|
| 27 |
|
| 28 |
|
| 29 |
-
# ---------- SIMPLE CONVERSION
|
| 30 |
def convert(file):
|
| 31 |
df = read_file(file)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
if "Time in Session (minutes)" not in df.columns:
|
| 34 |
return pd.DataFrame({"Message": ["No recognizable webinar structure"]})
|
| 35 |
|
|
@@ -64,7 +70,6 @@ def convert(file):
|
|
| 64 |
"Satisfaction (1-5)": [satisfaction]
|
| 65 |
})
|
| 66 |
|
| 67 |
-
# add health score
|
| 68 |
result["Health Score"] = (
|
| 69 |
(result["NPS Score"] * 0.4) +
|
| 70 |
(result["Completion Rate (%)"] * 0.3) +
|
|
@@ -76,21 +81,24 @@ def convert(file):
|
|
| 76 |
|
| 77 |
# ---------- CHART ----------
|
| 78 |
def make_chart(df):
|
| 79 |
-
if "Course Name" not in df.columns:
|
| 80 |
return None
|
| 81 |
|
| 82 |
fig, ax = plt.subplots()
|
| 83 |
ax.bar(df["Course Name"], df["Health Score"])
|
| 84 |
plt.xticks(rotation=45)
|
| 85 |
plt.title("Course Health Score")
|
|
|
|
| 86 |
return fig
|
| 87 |
|
| 88 |
|
| 89 |
# ---------- AI CHAT ----------
|
| 90 |
def chat_with_data(file, message, history):
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
df = read_file(file)
|
| 93 |
-
|
| 94 |
preview_text = df.head(20).to_string()
|
| 95 |
|
| 96 |
prompt = f"""
|
|
@@ -102,10 +110,10 @@ def chat_with_data(file, message, history):
|
|
| 102 |
User question:
|
| 103 |
{message}
|
| 104 |
|
| 105 |
-
|
| 106 |
- what this dataset is
|
| 107 |
-
- what
|
| 108 |
-
- how to
|
| 109 |
"""
|
| 110 |
|
| 111 |
response = client.models.generate_content(
|
|
@@ -118,11 +126,14 @@ def chat_with_data(file, message, history):
|
|
| 118 |
except Exception as e:
|
| 119 |
reply = str(e)
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
return history, history
|
| 123 |
|
| 124 |
|
| 125 |
-
# ----------
|
| 126 |
with gr.Blocks() as app:
|
| 127 |
gr.Markdown("# π Course Quality Tracker + AI Assistant")
|
| 128 |
|
|
@@ -136,7 +147,7 @@ with gr.Blocks() as app:
|
|
| 136 |
chart = gr.Plot()
|
| 137 |
|
| 138 |
gr.Markdown("## π€ Ask AI about your data")
|
| 139 |
-
chatbot = gr.Chatbot()
|
| 140 |
msg = gr.Textbox(label="Ask anything about your dataset")
|
| 141 |
|
| 142 |
# actions
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from google import genai
|
| 5 |
|
| 6 |
+
# π ADD YOUR GEMINI API KEY
|
| 7 |
client = genai.Client(api_key="AIzaSyCvvewIh6BVyULlEmw1QE75vWsEmu8QNH0")
|
| 8 |
|
| 9 |
|
| 10 |
# ---------- READ FILE ----------
|
| 11 |
def read_file(file):
|
| 12 |
try:
|
| 13 |
+
if file is None:
|
| 14 |
+
return pd.DataFrame()
|
| 15 |
+
|
| 16 |
if file.name.endswith(".xlsx"):
|
| 17 |
return pd.read_excel(file.name)
|
| 18 |
else:
|
|
|
|
| 29 |
return df.head(20)
|
| 30 |
|
| 31 |
|
| 32 |
+
# ---------- SIMPLE CONVERSION ----------
|
| 33 |
def convert(file):
|
| 34 |
df = read_file(file)
|
| 35 |
|
| 36 |
+
if df.empty:
|
| 37 |
+
return pd.DataFrame({"Message": ["No data"]})
|
| 38 |
+
|
| 39 |
if "Time in Session (minutes)" not in df.columns:
|
| 40 |
return pd.DataFrame({"Message": ["No recognizable webinar structure"]})
|
| 41 |
|
|
|
|
| 70 |
"Satisfaction (1-5)": [satisfaction]
|
| 71 |
})
|
| 72 |
|
|
|
|
| 73 |
result["Health Score"] = (
|
| 74 |
(result["NPS Score"] * 0.4) +
|
| 75 |
(result["Completion Rate (%)"] * 0.3) +
|
|
|
|
| 81 |
|
| 82 |
# ---------- CHART ----------
|
| 83 |
def make_chart(df):
|
| 84 |
+
if df is None or df.empty or "Course Name" not in df.columns:
|
| 85 |
return None
|
| 86 |
|
| 87 |
fig, ax = plt.subplots()
|
| 88 |
ax.bar(df["Course Name"], df["Health Score"])
|
| 89 |
plt.xticks(rotation=45)
|
| 90 |
plt.title("Course Health Score")
|
| 91 |
+
|
| 92 |
return fig
|
| 93 |
|
| 94 |
|
| 95 |
# ---------- AI CHAT ----------
|
| 96 |
def chat_with_data(file, message, history):
|
| 97 |
+
if history is None:
|
| 98 |
+
history = []
|
| 99 |
+
|
| 100 |
try:
|
| 101 |
df = read_file(file)
|
|
|
|
| 102 |
preview_text = df.head(20).to_string()
|
| 103 |
|
| 104 |
prompt = f"""
|
|
|
|
| 110 |
User question:
|
| 111 |
{message}
|
| 112 |
|
| 113 |
+
Explain:
|
| 114 |
- what this dataset is
|
| 115 |
+
- what issues exist
|
| 116 |
+
- how to structure it into course metrics
|
| 117 |
"""
|
| 118 |
|
| 119 |
response = client.models.generate_content(
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
reply = str(e)
|
| 128 |
|
| 129 |
+
# β
NEW CHAT FORMAT (FIXED)
|
| 130 |
+
history.append({"role": "user", "content": message})
|
| 131 |
+
history.append({"role": "assistant", "content": reply})
|
| 132 |
+
|
| 133 |
return history, history
|
| 134 |
|
| 135 |
|
| 136 |
+
# ---------- UI ----------
|
| 137 |
with gr.Blocks() as app:
|
| 138 |
gr.Markdown("# π Course Quality Tracker + AI Assistant")
|
| 139 |
|
|
|
|
| 147 |
chart = gr.Plot()
|
| 148 |
|
| 149 |
gr.Markdown("## π€ Ask AI about your data")
|
| 150 |
+
chatbot = gr.Chatbot(type="messages")
|
| 151 |
msg = gr.Textbox(label="Ask anything about your dataset")
|
| 152 |
|
| 153 |
# actions
|