Update written_module.py
Browse files- written_module.py +62 -84
written_module.py
CHANGED
|
@@ -2,11 +2,9 @@ import gradio as gr
|
|
| 2 |
import uuid
|
| 3 |
import os
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
from gtts import gTTS
|
| 6 |
from openai import OpenAI
|
| 7 |
from app_utils import (
|
| 8 |
LANG_CODES, save_to_db, fetch_user_sessions,
|
| 9 |
-
convert_to_wav, transcribe_audio,
|
| 10 |
parse_scores_from_feedback, generate_progress_summary,
|
| 11 |
build_score_comparison_data, render_score_chart,
|
| 12 |
build_trend_data, render_trend_chart
|
|
@@ -14,48 +12,45 @@ from app_utils import (
|
|
| 14 |
|
| 15 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
focus_str = ", ".join(focus_areas) if focus_areas else "Clarity, Structure,
|
| 19 |
-
history_section = f"\n\
|
| 20 |
|
| 21 |
prompt = f"""
|
| 22 |
-
You are a
|
| 23 |
|
| 24 |
-
|
| 25 |
{focus_str}
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
- One motivational line to end with.
|
| 32 |
-
|
| 33 |
-
Transcript:
|
| 34 |
-
{transcript}
|
| 35 |
{history_section}
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
response = client.chat.completions.create(
|
| 39 |
model="gpt-4",
|
| 40 |
messages=[
|
| 41 |
-
{"role": "system", "content": f"You are a
|
| 42 |
{"role": "user", "content": prompt}
|
| 43 |
],
|
| 44 |
temperature=0.7
|
| 45 |
)
|
| 46 |
return response.choices[0].message.content
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
prompt = f"""
|
| 50 |
-
Keep the meaning and tone the same, but improve clarity and structure.
|
| 51 |
|
| 52 |
-
|
| 53 |
-
{
|
| 54 |
"""
|
| 55 |
response = client.chat.completions.create(
|
| 56 |
model="gpt-4",
|
| 57 |
messages=[
|
| 58 |
-
{"role": "system", "content": f"Reply in {language}. Provide only the improved version
|
| 59 |
{"role": "user", "content": prompt}
|
| 60 |
]
|
| 61 |
)
|
|
@@ -68,90 +63,73 @@ def render_empty_chart(title):
|
|
| 68 |
ax.axis('off')
|
| 69 |
return fig
|
| 70 |
|
| 71 |
-
def
|
| 72 |
-
with gr.Column() as
|
| 73 |
gr.Markdown("""
|
| 74 |
<div id="header" style="text-align: center;">
|
| 75 |
<img src="images/chatternest_logo.png" width="100">
|
| 76 |
-
<h2>
|
| 77 |
-
<p>
|
| 78 |
</div>
|
| 79 |
""")
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 83 |
-
goal_dropdown = gr.Dropdown(label="π― Goal", choices=["
|
| 84 |
-
focus_checkboxes = gr.CheckboxGroup(label="π§ Focus Areas", choices=["Clarity", "
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
transcript_box = gr.Textbox(label="π Transcript", interactive=False)
|
| 91 |
-
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False)
|
| 92 |
-
hidden_transcript = gr.Textbox(visible=False)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
example_box = gr.Textbox(label="π£ Suggested Improvement", visible=True)
|
| 99 |
-
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "π Transcript", "π¬ Feedback"])
|
| 100 |
score_chart = gr.Plot(label="π Score Comparison")
|
| 101 |
-
trend_chart = gr.Plot(label="π
|
| 102 |
milestone_box = gr.Markdown(visible=False)
|
| 103 |
|
| 104 |
-
def
|
| 105 |
-
if
|
| 106 |
-
return ""
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
nickname = nickname.value # Handle gr.Textbox or gr.State
|
| 110 |
-
|
| 111 |
-
wav_path = convert_to_wav(audio_file)
|
| 112 |
-
transcript = transcribe_audio(wav_path)
|
| 113 |
-
previous_sessions = fetch_user_sessions(nickname)
|
| 114 |
-
previous_transcript = previous_sessions[-1].transcript if previous_sessions else None
|
| 115 |
-
previous_feedback = previous_sessions[-1].feedback if previous_sessions else None
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
milestone = f"π Congrats on completing **{session_count} sessions**!"
|
| 126 |
-
feedback_text += f"\n\n{milestone}"
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
tts.save(mp3_path)
|
| 131 |
|
| 132 |
-
save_to_db(nickname,
|
| 133 |
sessions = fetch_user_sessions(nickname)
|
| 134 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 135 |
|
| 136 |
-
score_plot = render_score_chart(build_score_comparison_data(
|
| 137 |
-
dates,
|
| 138 |
-
trend_plot = render_trend_chart(dates,
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
fn=tutor_feedback,
|
| 144 |
-
inputs=[audio_input, language_dropdown, goal_dropdown, focus_checkboxes, nickname_input],
|
| 145 |
-
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, history_table, score_chart, trend_chart, milestone_box],
|
| 146 |
-
show_progress="minimal"
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
try_again.click(fn=lambda: ("", "", None, "", "", render_empty_chart("π Score Comparison"), render_empty_chart("π Tone Progress"), gr.update(visible=False)),
|
| 150 |
-
inputs=None,
|
| 151 |
-
outputs=[transcript_box, feedback_box, audio_output, hidden_transcript, example_box, score_chart, trend_chart, milestone_box])
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
return
|
|
|
|
| 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
|
|
|
|
| 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
|
| 41 |
)
|
| 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}
|
| 49 |
"""
|
| 50 |
response = client.chat.completions.create(
|
| 51 |
model="gpt-4",
|
| 52 |
messages=[
|
| 53 |
+
{"role": "system", "content": f"Reply in {language}. Provide only the improved version."},
|
| 54 |
{"role": "user", "content": prompt}
|
| 55 |
]
|
| 56 |
)
|
|
|
|
| 63 |
ax.axis('off')
|
| 64 |
return fig
|
| 65 |
|
| 66 |
+
def written_dashboard(nickname_input):
|
| 67 |
+
with gr.Column() as written_panel:
|
| 68 |
gr.Markdown("""
|
| 69 |
<div id="header" style="text-align: center;">
|
| 70 |
<img src="images/chatternest_logo.png" width="100">
|
| 71 |
+
<h2>βοΈ Written Communication with <strong>Chatter the Owl</strong></h2>
|
| 72 |
+
<p>Paste or upload your writing. Select your goal and focus, and receive personalized feedback with progress tracking.</p>
|
| 73 |
</div>
|
| 74 |
""")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
language_dropdown = gr.Dropdown(label="π Language", choices=list(LANG_CODES.keys()), value="English")
|
| 78 |
+
goal_dropdown = gr.Dropdown(label="π― Writing Goal", choices=["Essay for school", "Job application", "Blog post", "Creative writing", "General improvement"], value="General improvement")
|
| 79 |
+
focus_checkboxes = gr.CheckboxGroup(label="π§ Focus Areas", choices=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"], value=["Clarity", "Organization", "Grammar", "Vocabulary", "Tone", "Creativity"])
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
+
input_text = gr.Textbox(label="π Your Writing", lines=8, elem_id="input_text", elem_classes=["text_area"])
|
| 83 |
+
submit_button = gr.Button("π Submit", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
file_input = gr.File(label="π Upload Text File", file_types=[".txt", ".md"])
|
| 86 |
+
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False, elem_id="feedback_box")
|
| 87 |
+
improved_box = gr.Textbox(label="π― Improved Version", interactive=False, elem_id="improved_box")
|
| 88 |
+
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "βοΈ Text", "π Feedback"])
|
|
|
|
|
|
|
| 89 |
score_chart = gr.Plot(label="π Score Comparison")
|
| 90 |
+
trend_chart = gr.Plot(label="π Clarity Progress")
|
| 91 |
milestone_box = gr.Markdown(visible=False)
|
| 92 |
|
| 93 |
+
def extract_text(file):
|
| 94 |
+
if file is None:
|
| 95 |
+
return ""
|
| 96 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
| 97 |
+
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
file_input.change(fn=extract_text, inputs=file_input, outputs=input_text)
|
| 100 |
|
| 101 |
+
def tutor_written(text, language, goal, focus_areas, nickname):
|
| 102 |
+
if hasattr(nickname, "value"):
|
| 103 |
+
nickname = nickname.value
|
| 104 |
+
if not text.strip():
|
| 105 |
+
return "", "", [], render_empty_chart("π Score Comparison"), render_empty_chart("π Clarity Progress"), gr.update(visible=False)
|
| 106 |
|
| 107 |
+
sessions = fetch_user_sessions(nickname)
|
| 108 |
+
previous_text = sessions[-1].transcript if sessions else None
|
| 109 |
+
previous_feedback = sessions[-1].feedback if sessions else None
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
feedback = generate_written_feedback(text, language, goal, focus_areas, previous_text)
|
| 112 |
+
improved = improve_written_text(text, language)
|
|
|
|
| 113 |
|
| 114 |
+
save_to_db(nickname, text, feedback, language)
|
| 115 |
sessions = fetch_user_sessions(nickname)
|
| 116 |
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 117 |
|
| 118 |
+
score_plot = render_score_chart(build_score_comparison_data(feedback, previous_feedback)) if previous_feedback else render_empty_chart("π Score Comparison")
|
| 119 |
+
dates, clarity_scores = build_trend_data(sessions, "Clarity")
|
| 120 |
+
trend_plot = render_trend_chart(dates, clarity_scores, "Clarity") if clarity_scores else render_empty_chart("π Clarity Progress")
|
| 121 |
|
| 122 |
+
milestone_msg = ""
|
| 123 |
+
if len(sessions) in [3, 5, 10]:
|
| 124 |
+
milestone_msg = f"π Youβve completed **{len(sessions)} writing sessions**!"
|
| 125 |
+
feedback += f"\n\n{milestone_msg}"
|
| 126 |
|
| 127 |
+
return feedback, improved, session_table, score_plot, trend_plot, gr.update(visible=bool(milestone_msg), value=milestone_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
submit_button.click(
|
| 130 |
+
fn=tutor_written,
|
| 131 |
+
inputs=[input_text, language_dropdown, goal_dropdown, focus_checkboxes, nickname_input],
|
| 132 |
+
outputs=[feedback_box, improved_box, history_table, score_chart, trend_chart, milestone_box]
|
| 133 |
+
)
|
| 134 |
|
| 135 |
+
return written_panel
|