Update written_module.py
Browse files- written_module.py +26 -49
written_module.py
CHANGED
|
@@ -2,41 +2,39 @@ import gradio as gr
|
|
| 2 |
import uuid
|
| 3 |
import os
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
from app_utils import (
|
| 6 |
LANG_CODES, save_to_db, fetch_user_sessions,
|
| 7 |
parse_scores_from_feedback, generate_progress_summary,
|
| 8 |
build_score_comparison_data, render_score_chart,
|
| 9 |
build_trend_data, render_trend_chart
|
| 10 |
)
|
| 11 |
-
from openai import OpenAI
|
| 12 |
|
| 13 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 14 |
|
| 15 |
def generate_written_feedback(text, language, goal="general improvement", focus_areas=None, previous_text=None):
|
| 16 |
-
focus_str = ", ".join(focus_areas) if focus_areas else "Clarity, Structure, Grammar, Style, Tone,
|
| 17 |
history_section = f"\n\nCompare this to their previous writing:\n{previous_text}" if previous_text else ""
|
| 18 |
|
| 19 |
prompt = f"""
|
| 20 |
-
You are a
|
| 21 |
|
| 22 |
-
First, return a JSON object
|
| 23 |
{focus_str}
|
| 24 |
|
| 25 |
-
Then
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
-
|
| 29 |
-
- One motivational sentence
|
| 30 |
|
| 31 |
-
|
| 32 |
{text}
|
| 33 |
{history_section}
|
| 34 |
"""
|
| 35 |
-
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
model="gpt-4",
|
| 38 |
messages=[
|
| 39 |
-
{"role": "system", "content": f"You are a
|
| 40 |
{"role": "user", "content": prompt}
|
| 41 |
],
|
| 42 |
temperature=0.7
|
|
@@ -44,7 +42,7 @@ Text:
|
|
| 44 |
return response.choices[0].message.content
|
| 45 |
|
| 46 |
def improve_written_text(text, language):
|
| 47 |
-
prompt = f"""
|
| 48 |
|
| 49 |
Text:
|
| 50 |
{text}
|
|
@@ -64,39 +62,24 @@ def written_dashboard(nickname):
|
|
| 64 |
<div id="header" style="text-align: center;">
|
| 65 |
<img src="file/images/chatter_owl.png" width="120">
|
| 66 |
<h2>βοΈ Written Communication with <strong>Chatter the Owl</strong></h2>
|
| 67 |
-
<p>Paste or upload writing
|
| 68 |
</div>
|
| 69 |
""")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
label="
|
| 75 |
-
choices=[
|
| 76 |
-
"Essay for school",
|
| 77 |
-
"Job application",
|
| 78 |
-
"Public blog post",
|
| 79 |
-
"Creative writing",
|
| 80 |
-
"General improvement"
|
| 81 |
-
],
|
| 82 |
-
value="General improvement"
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
focus_checkboxes = gr.CheckboxGroup(
|
| 86 |
-
label="π§ Areas to Focus On",
|
| 87 |
-
choices=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"],
|
| 88 |
-
value=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"]
|
| 89 |
-
)
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
-
input_text = gr.Textbox(
|
| 93 |
file_input = gr.File(label="π Upload Text File", file_types=[".txt", ".md"])
|
| 94 |
|
| 95 |
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False, elem_id="feedback_box")
|
| 96 |
improved_box = gr.Textbox(label="π― Improved Version", interactive=False, elem_id="improved_box")
|
| 97 |
-
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "βοΈ
|
| 98 |
-
score_chart = gr.Plot(label="π
|
| 99 |
-
trend_chart = gr.Plot(label="π
|
| 100 |
milestone_box = gr.Markdown(visible=False)
|
| 101 |
|
| 102 |
def extract_text(file):
|
|
@@ -117,27 +100,21 @@ def written_dashboard(nickname):
|
|
| 117 |
|
| 118 |
feedback = generate_written_feedback(text, language, goal, focus_areas, previous_text)
|
| 119 |
improved = improve_written_text(text, language)
|
| 120 |
-
save_to_db(nickname, text, feedback, language)
|
| 121 |
|
|
|
|
| 122 |
sessions = fetch_user_sessions(nickname)
|
| 123 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
chart_plot = render_score_chart(chart_data) if chart_data else None
|
| 128 |
-
|
| 129 |
-
# Trend chart
|
| 130 |
-
dates, clarity_scores = build_trend_data(sessions, category="Clarity")
|
| 131 |
trend_plot = render_trend_chart(dates, clarity_scores, "Clarity") if clarity_scores else None
|
| 132 |
|
| 133 |
-
# Milestone
|
| 134 |
-
session_count = len(sessions)
|
| 135 |
milestone_msg = ""
|
| 136 |
-
if
|
| 137 |
-
milestone_msg = f"π You
|
| 138 |
feedback += f"\n\n{milestone_msg}"
|
| 139 |
|
| 140 |
-
return feedback, improved, session_table,
|
| 141 |
|
| 142 |
input_text.change(
|
| 143 |
fn=tutor_written,
|
|
|
|
| 2 |
import uuid
|
| 3 |
import os
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from openai import OpenAI
|
| 6 |
from app_utils import (
|
| 7 |
LANG_CODES, save_to_db, fetch_user_sessions,
|
| 8 |
parse_scores_from_feedback, generate_progress_summary,
|
| 9 |
build_score_comparison_data, render_score_chart,
|
| 10 |
build_trend_data, render_trend_chart
|
| 11 |
)
|
|
|
|
| 12 |
|
| 13 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 14 |
|
| 15 |
def generate_written_feedback(text, language, goal="general improvement", focus_areas=None, previous_text=None):
|
| 16 |
+
focus_str = ", ".join(focus_areas) if focus_areas else "Clarity, Structure, Grammar, Style, Tone, Creativity"
|
| 17 |
history_section = f"\n\nCompare this to their previous writing:\n{previous_text}" if previous_text else ""
|
| 18 |
|
| 19 |
prompt = f"""
|
| 20 |
+
You are a writing coach helping a student improve their writing for the following goal: **{goal}**.
|
| 21 |
|
| 22 |
+
First, return a JSON object with scores (0β10) for each of these:
|
| 23 |
{focus_str}
|
| 24 |
|
| 25 |
+
Then write:
|
| 26 |
+
- A brief explanation per score
|
| 27 |
+
- A summary of strengths and improvements
|
| 28 |
+
- One motivational line
|
|
|
|
| 29 |
|
| 30 |
+
Current text:
|
| 31 |
{text}
|
| 32 |
{history_section}
|
| 33 |
"""
|
|
|
|
| 34 |
response = client.chat.completions.create(
|
| 35 |
model="gpt-4",
|
| 36 |
messages=[
|
| 37 |
+
{"role": "system", "content": f"You are a helpful writing tutor responding in {language}."},
|
| 38 |
{"role": "user", "content": prompt}
|
| 39 |
],
|
| 40 |
temperature=0.7
|
|
|
|
| 42 |
return response.choices[0].message.content
|
| 43 |
|
| 44 |
def improve_written_text(text, language):
|
| 45 |
+
prompt = f"""Improve this writing by refining structure, grammar, and clarity β without changing its meaning.
|
| 46 |
|
| 47 |
Text:
|
| 48 |
{text}
|
|
|
|
| 62 |
<div id="header" style="text-align: center;">
|
| 63 |
<img src="file/images/chatter_owl.png" width="120">
|
| 64 |
<h2>βοΈ Written Communication with <strong>Chatter the Owl</strong></h2>
|
| 65 |
+
<p>Paste or upload your writing. Select your goal and focus, and receive detailed feedback, improvement, and progress tracking.</p>
|
| 66 |
</div>
|
| 67 |
""")
|
| 68 |
|
| 69 |
+
with gr.Row():
|
| 70 |
+
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 71 |
+
goal_dropdown = gr.Dropdown(label="π― Writing Goal", choices=["Essay for school", "Job application", "Blog post", "Creative writing", "General improvement"], value="General improvement")
|
| 72 |
+
focus_checkboxes = gr.CheckboxGroup(label="π§ Focus Areas", choices=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"], value=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
+
input_text = gr.Textbox(label="π Your Writing", lines=8, elem_id="input_text", elem_classes=["text_area"])
|
| 76 |
file_input = gr.File(label="π Upload Text File", file_types=[".txt", ".md"])
|
| 77 |
|
| 78 |
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False, elem_id="feedback_box")
|
| 79 |
improved_box = gr.Textbox(label="π― Improved Version", interactive=False, elem_id="improved_box")
|
| 80 |
+
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "βοΈ Text", "π Feedback"])
|
| 81 |
+
score_chart = gr.Plot(label="π Score Comparison")
|
| 82 |
+
trend_chart = gr.Plot(label="π Clarity Progress")
|
| 83 |
milestone_box = gr.Markdown(visible=False)
|
| 84 |
|
| 85 |
def extract_text(file):
|
|
|
|
| 100 |
|
| 101 |
feedback = generate_written_feedback(text, language, goal, focus_areas, previous_text)
|
| 102 |
improved = improve_written_text(text, language)
|
|
|
|
| 103 |
|
| 104 |
+
save_to_db(nickname, text, feedback, language)
|
| 105 |
sessions = fetch_user_sessions(nickname)
|
| 106 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 107 |
|
| 108 |
+
score_plot = render_score_chart(build_score_comparison_data(feedback, previous_feedback)) if previous_feedback else None
|
| 109 |
+
dates, clarity_scores = build_trend_data(sessions, "Clarity")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
trend_plot = render_trend_chart(dates, clarity_scores, "Clarity") if clarity_scores else None
|
| 111 |
|
|
|
|
|
|
|
| 112 |
milestone_msg = ""
|
| 113 |
+
if len(sessions) in [3, 5, 10]:
|
| 114 |
+
milestone_msg = f"π Youβve completed **{len(sessions)} writing sessions**!"
|
| 115 |
feedback += f"\n\n{milestone_msg}"
|
| 116 |
|
| 117 |
+
return feedback, improved, session_table, score_plot, trend_plot, gr.update(visible=bool(milestone_msg), value=milestone_msg)
|
| 118 |
|
| 119 |
input_text.change(
|
| 120 |
fn=tutor_written,
|