Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import os
|
|
| 3 |
import datetime
|
| 4 |
import json
|
| 5 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# === Import Groq API ===
|
| 8 |
from groq import Groq
|
|
@@ -111,6 +115,76 @@ def get_history():
|
|
| 111 |
return "\n".join([f"[{h['timestamp']}] {h['input']} β {h['response']}" for h in history])
|
| 112 |
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# === Gradio Interface ===
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("# πβοΈ Language Buddy")
|
|
@@ -137,8 +211,20 @@ with gr.Blocks() as demo:
|
|
| 137 |
refresh_btn = gr.Button("Refresh History")
|
| 138 |
refresh_btn.click(get_history, outputs=history_box)
|
| 139 |
|
|
|
|
|
|
|
| 140 |
with gr.Tab("Progress π"):
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
# === Launch ===
|
|
|
|
| 3 |
import datetime
|
| 4 |
import json
|
| 5 |
import tempfile
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import io
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
|
| 11 |
# === Import Groq API ===
|
| 12 |
from groq import Groq
|
|
|
|
| 115 |
return "\n".join([f"[{h['timestamp']}] {h['input']} β {h['response']}" for h in history])
|
| 116 |
|
| 117 |
|
| 118 |
+
# === Progress Functions ===
|
| 119 |
+
def calculate_progress():
|
| 120 |
+
# Load history
|
| 121 |
+
with open(HISTORY_FILE, "r") as f:
|
| 122 |
+
history = json.load(f)
|
| 123 |
+
|
| 124 |
+
if not history:
|
| 125 |
+
return "No progress yet! Try practicing first.", None, None, "π₯ 0-day streak"
|
| 126 |
+
|
| 127 |
+
# --- Error Trend (Mistakes over time) ---
|
| 128 |
+
dates = [h["timestamp"].split(" ")[0] for h in history]
|
| 129 |
+
mistake_counts = [len(h["response"].split()) - len(h["input"].split()) for h in history] # rough proxy
|
| 130 |
+
|
| 131 |
+
plt.figure(figsize=(5,3))
|
| 132 |
+
plt.plot(dates, mistake_counts, marker="o")
|
| 133 |
+
plt.xticks(rotation=45)
|
| 134 |
+
plt.title("Mistakes Over Time")
|
| 135 |
+
plt.tight_layout()
|
| 136 |
+
trend_img = save_plot_to_base64()
|
| 137 |
+
|
| 138 |
+
# --- Error Categories ---
|
| 139 |
+
categories = {"Grammar":0, "Spelling":0, "Vocabulary":0}
|
| 140 |
+
for h in history:
|
| 141 |
+
resp = h["response"].lower()
|
| 142 |
+
if "grammar" in resp: categories["Grammar"] += 1
|
| 143 |
+
if "spelling" in resp: categories["Spelling"] += 1
|
| 144 |
+
if "word" in resp: categories["Vocabulary"] += 1
|
| 145 |
+
|
| 146 |
+
plt.figure(figsize=(4,3))
|
| 147 |
+
plt.bar(categories.keys(), categories.values(), color="orange")
|
| 148 |
+
plt.title("Error Categories")
|
| 149 |
+
plt.tight_layout()
|
| 150 |
+
cat_img = save_plot_to_base64()
|
| 151 |
+
|
| 152 |
+
# --- Streak Counter ---
|
| 153 |
+
practiced_days = sorted(set(dates))
|
| 154 |
+
streak = 1
|
| 155 |
+
for i in range(len(practiced_days)-1, 0, -1):
|
| 156 |
+
d1 = datetime.datetime.strptime(practiced_days[i], "%Y-%m-%d")
|
| 157 |
+
d2 = datetime.datetime.strptime(practiced_days[i-1], "%Y-%m-%d")
|
| 158 |
+
if (d1 - d2).days == 1:
|
| 159 |
+
streak += 1
|
| 160 |
+
else:
|
| 161 |
+
break
|
| 162 |
+
|
| 163 |
+
return "π Progress Overview", trend_img, cat_img, f"π₯ {streak}-day streak"
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def save_plot_to_base64():
|
| 167 |
+
buf = io.BytesIO()
|
| 168 |
+
plt.savefig(buf, format="png")
|
| 169 |
+
buf.seek(0)
|
| 170 |
+
encoded = base64.b64encode(buf.read()).decode("utf-8")
|
| 171 |
+
buf.close()
|
| 172 |
+
return "data:image/png;base64," + encoded
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
# === Updated Progress Tab ===
|
| 176 |
+
with gr.Tab("Progress π"):
|
| 177 |
+
gr.Markdown("### Your Learning Progress")
|
| 178 |
+
|
| 179 |
+
progress_label = gr.Label()
|
| 180 |
+
trend_plot = gr.Image(type="pil", label="Error Trend")
|
| 181 |
+
cat_plot = gr.Image(type="pil", label="Error Categories")
|
| 182 |
+
streak_label = gr.Label()
|
| 183 |
+
|
| 184 |
+
load_btn = gr.Button("Load Progress")
|
| 185 |
+
load_btn.click(calculate_progress, outputs=[progress_label, trend_plot, cat_plot, streak_label])
|
| 186 |
+
|
| 187 |
+
|
| 188 |
# === Gradio Interface ===
|
| 189 |
with gr.Blocks() as demo:
|
| 190 |
gr.Markdown("# πβοΈ Language Buddy")
|
|
|
|
| 211 |
refresh_btn = gr.Button("Refresh History")
|
| 212 |
refresh_btn.click(get_history, outputs=history_box)
|
| 213 |
|
| 214 |
+
# with gr.Tab("Progress π"):
|
| 215 |
+
# gr.Markdown("Error tracking & progress visualization coming soon!")
|
| 216 |
with gr.Tab("Progress π"):
|
| 217 |
+
gr.Markdown("### π Your Progress Overview")
|
| 218 |
+
|
| 219 |
+
with gr.Row():
|
| 220 |
+
error_plot = gr.Image(type="pil", label="Error Trend")
|
| 221 |
+
error_stats = gr.Textbox(label="Error Categories", interactive=False, lines=6)
|
| 222 |
+
|
| 223 |
+
streak_counter = gr.Textbox(label="π₯ Current Streak", interactive=False)
|
| 224 |
+
|
| 225 |
+
refresh_progress = gr.Button("Refresh Progress")
|
| 226 |
+
refresh_progress.click(calculate_progress, outputs=[error_plot, error_stats, streak_counter])
|
| 227 |
+
|
| 228 |
|
| 229 |
|
| 230 |
# === Launch ===
|