Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,6 +58,10 @@ def analyze_text(user_input):
|
|
| 58 |
2. Provide a formal version.
|
| 59 |
3. Provide an informal version.
|
| 60 |
4. Highlight difficult/new words.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
response = client.chat.completions.create(
|
|
@@ -68,13 +72,26 @@ def analyze_text(user_input):
|
|
| 68 |
corrected_text = response.choices[0].message.content # β
fixed
|
| 69 |
# corrected_text = response.choices[0].message.content
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Save history
|
| 80 |
with open(HISTORY_FILE, "r+") as f:
|
|
@@ -82,12 +99,12 @@ def analyze_text(user_input):
|
|
| 82 |
history.append({
|
| 83 |
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 84 |
"input": user_input,
|
| 85 |
-
"response":
|
| 86 |
})
|
| 87 |
f.seek(0)
|
| 88 |
json.dump(history, f, indent=2)
|
| 89 |
|
| 90 |
-
return
|
| 91 |
except Exception as e:
|
| 92 |
return f"β Error in analyze_text: {str(e)}"
|
| 93 |
|
|
@@ -178,7 +195,6 @@ def save_plot_to_base64():
|
|
| 178 |
buf.close()
|
| 179 |
return "data:image/png;base64," + encoded
|
| 180 |
|
| 181 |
-
import datetime
|
| 182 |
|
| 183 |
# keep progress data in memory (you can later persist to file/db if needed)
|
| 184 |
progress_data = {
|
|
|
|
| 58 |
2. Provide a formal version.
|
| 59 |
3. Provide an informal version.
|
| 60 |
4. Highlight difficult/new words.
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
Finally, output a JSON object in this format (at the very end, nothing else):
|
| 64 |
+
<stats>{{"correct": true or false}}</stats>
|
| 65 |
"""
|
| 66 |
|
| 67 |
response = client.chat.completions.create(
|
|
|
|
| 72 |
corrected_text = response.choices[0].message.content # β
fixed
|
| 73 |
# corrected_text = response.choices[0].message.content
|
| 74 |
|
| 75 |
+
# β
Extract correctness
|
| 76 |
+
is_correct = False
|
| 77 |
+
if "<stats>" in corrected_text and "</stats>" in corrected_text:
|
| 78 |
+
try:
|
| 79 |
+
stats_str = corrected_text.split("<stats>")[1].split("</stats>")[0]
|
| 80 |
+
stats = json.loads(stats_str)
|
| 81 |
+
is_correct = stats.get("correct", False)
|
| 82 |
+
except Exception:
|
| 83 |
+
is_correct = False
|
| 84 |
|
| 85 |
+
update_daily_stats(is_correct=is_correct)
|
| 86 |
+
|
| 87 |
+
# β
Clean output for user (remove hidden stats)
|
| 88 |
+
cleaned_text = corrected_text.split("<stats>")[0].strip()
|
| 89 |
+
# if "β
Correct" in response or "No errors" in response:
|
| 90 |
+
# update_daily_stats(is_correct=True)
|
| 91 |
+
# else:
|
| 92 |
+
# update_daily_stats(is_correct=False)
|
| 93 |
+
|
| 94 |
+
# return corrected_text # not return response
|
| 95 |
|
| 96 |
# Save history
|
| 97 |
with open(HISTORY_FILE, "r+") as f:
|
|
|
|
| 99 |
history.append({
|
| 100 |
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 101 |
"input": user_input,
|
| 102 |
+
"response": cleaned_text
|
| 103 |
})
|
| 104 |
f.seek(0)
|
| 105 |
json.dump(history, f, indent=2)
|
| 106 |
|
| 107 |
+
return cleaned_text
|
| 108 |
except Exception as e:
|
| 109 |
return f"β Error in analyze_text: {str(e)}"
|
| 110 |
|
|
|
|
| 195 |
buf.close()
|
| 196 |
return "data:image/png;base64," + encoded
|
| 197 |
|
|
|
|
| 198 |
|
| 199 |
# keep progress data in memory (you can later persist to file/db if needed)
|
| 200 |
progress_data = {
|