lalaru commited on
Commit
81b4bb3
Β·
verified Β·
1 Parent(s): 4fd85c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -45
app.py CHANGED
@@ -67,6 +67,12 @@ def analyze_text(user_input):
67
 
68
  corrected_text = response.choices[0].message.content # βœ… fixed
69
 
 
 
 
 
 
 
70
  # Save history
71
  with open(HISTORY_FILE, "r+") as f:
72
  history = json.load(f)
@@ -169,57 +175,58 @@ def save_plot_to_base64():
169
  buf.close()
170
  return "data:image/png;base64," + encoded
171
 
 
172
 
173
- # === Daily Tracking ===
174
- DAILY_STATS = {
175
- "date": str(datetime.date.today()),
176
- "attempts": 0,
177
- "correct": 0,
178
- "errors": 0,
179
- "current_streak": 0,
180
- "badge": None
181
  }
182
 
183
  def update_daily_stats(is_correct: bool):
184
- """Update stats after each attempt."""
185
  today = str(datetime.date.today())
186
 
187
- # reset if date changes
188
- if DAILY_STATS["date"] != today:
189
- DAILY_STATS.update({
190
- "date": today,
191
- "attempts": 0,
192
- "correct": 0,
193
- "errors": 0,
194
- "current_streak": 0,
195
- "badge": None
196
- })
197
-
198
- DAILY_STATS["attempts"] += 1
199
-
200
  if is_correct:
201
- DAILY_STATS["correct"] += 1
202
- DAILY_STATS["current_streak"] += 1
203
- if DAILY_STATS["current_streak"] >= 5:
204
- DAILY_STATS["badge"] = "πŸ… 5-in-a-row Streak!"
 
205
  else:
206
- DAILY_STATS["errors"] += 1
207
- DAILY_STATS["current_streak"] = 0 # reset streak
208
-
209
- return get_progress_summary()
210
 
211
  def get_progress_summary():
212
- """Return formatted progress text + badge."""
213
- text = (
214
- f"πŸ“… Date: {DAILY_STATS['date']}\n"
215
- f"πŸ”’ Attempts: {DAILY_STATS['attempts']}\n"
216
- f"βœ… Correct: {DAILY_STATS['correct']}\n"
217
- f"❌ Errors: {DAILY_STATS['errors']}\n"
218
- f"πŸ”₯ Streak: {DAILY_STATS['current_streak']}\n"
 
 
 
 
 
 
219
  )
220
- if DAILY_STATS["badge"]:
221
- text += f"\nπŸ† Badge Earned: {DAILY_STATS['badge']}"
222
- return text
223
 
224
  # # === Inside Gradio UI ===
225
  # with gr.Tab("Progress πŸ“ˆ"):
@@ -260,12 +267,19 @@ with gr.Blocks() as demo:
260
  # with gr.Tab("Progress πŸ“ˆ"):
261
  # gr.Markdown("Error tracking & progress visualization coming soon!")
262
  with gr.Tab("Progress πŸ“ˆ"):
263
- gr.Markdown("### πŸ“Š Your Daily Progress")
264
- progress_box = gr.Textbox(label="Today’s Stats", interactive=False, lines=8)
265
 
266
- refresh_progress = gr.Button("πŸ”„ Refresh Progress")
267
- refresh_progress.click(get_progress_summary, outputs=progress_box)
268
-
 
 
 
 
 
 
 
 
269
 
270
  # === Launch ===
271
  demo.launch()
 
67
 
68
  corrected_text = response.choices[0].message.content # βœ… fixed
69
 
70
+ if "βœ… Correct" in response or "No errors" in response:
71
+ update_daily_stats(is_correct=True)
72
+ else:
73
+ update_daily_stats(is_correct=False)
74
+
75
+ return response
76
  # Save history
77
  with open(HISTORY_FILE, "r+") as f:
78
  history = json.load(f)
 
175
  buf.close()
176
  return "data:image/png;base64," + encoded
177
 
178
+ import datetime
179
 
180
+ # keep progress data in memory (you can later persist to file/db if needed)
181
+ progress_data = {
182
+ "today": str(datetime.date.today()),
183
+ "correct_count": 0,
184
+ "incorrect_count": 0,
185
+ "streak": 0,
186
+ "badges": []
 
187
  }
188
 
189
  def update_daily_stats(is_correct: bool):
190
+ """Update today's progress based on correctness."""
191
  today = str(datetime.date.today())
192
 
193
+ # Reset stats if a new day has started
194
+ if progress_data["today"] != today:
195
+ progress_data["today"] = today
196
+ progress_data["correct_count"] = 0
197
+ progress_data["incorrect_count"] = 0
198
+ progress_data["streak"] = 0
199
+ progress_data["badges"] = []
200
+
 
 
 
 
 
201
  if is_correct:
202
+ progress_data["correct_count"] += 1
203
+ progress_data["streak"] += 1
204
+ # Award badge if streak hits 5
205
+ if progress_data["streak"] == 5:
206
+ progress_data["badges"].append("πŸ… 5-in-a-row Champion")
207
  else:
208
+ progress_data["incorrect_count"] += 1
209
+ progress_data["streak"] = 0
210
+
 
211
 
212
  def get_progress_summary():
213
+ """Return a formatted summary for Gradio UI."""
214
+ today = progress_data["today"]
215
+ correct = progress_data["correct_count"]
216
+ incorrect = progress_data["incorrect_count"]
217
+ streak = progress_data["streak"]
218
+ badges = ", ".join(progress_data["badges"]) if progress_data["badges"] else "None yet"
219
+
220
+ summary = (
221
+ f"πŸ“… Date: {today}\n"
222
+ f"βœ… Correct: {correct}\n"
223
+ f"❌ Incorrect: {incorrect}\n"
224
+ f"πŸ”₯ Current Streak: {streak}\n"
225
+ f"πŸ… Badges: {badges}"
226
  )
227
+ return summary
228
+
229
+
230
 
231
  # # === Inside Gradio UI ===
232
  # with gr.Tab("Progress πŸ“ˆ"):
 
267
  # with gr.Tab("Progress πŸ“ˆ"):
268
  # gr.Markdown("Error tracking & progress visualization coming soon!")
269
  with gr.Tab("Progress πŸ“ˆ"):
270
+ gr.Markdown("### πŸ“Š Daily Progress Tracker")
 
271
 
272
+ progress_box = gr.Textbox(
273
+ label="Today's Progress",
274
+ interactive=False,
275
+ lines=8,
276
+ value="No progress yet. Try practicing!"
277
+ )
278
+
279
+ refresh_btn = gr.Button("πŸ”„ Refresh Progress")
280
+
281
+ # βœ… bind refresh button here
282
+ refresh_btn.click(get_progress_summary, outputs=progress_box)
283
 
284
  # === Launch ===
285
  demo.launch()