Create written_module.py
Browse files- written_module.py +103 -0
written_module.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import uuid
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
from app_utils import (
|
| 5 |
+
LANG_CODES, client, save_to_db, fetch_user_sessions
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def generate_written_feedback(text, language):
|
| 10 |
+
prompt = f"""You are a writing tutor. Please respond in [language={language}].
|
| 11 |
+
Evaluate the user's writing on the following 6 criteria:
|
| 12 |
+
1. Clarity & Conciseness
|
| 13 |
+
2. Organization & Flow
|
| 14 |
+
3. Grammar & Mechanics
|
| 15 |
+
4. Vocabulary & Style
|
| 16 |
+
5. Tone Appropriateness
|
| 17 |
+
6. Originality & Creativity
|
| 18 |
+
|
| 19 |
+
For each:
|
| 20 |
+
- Give a score out of 10
|
| 21 |
+
- Write a short explanation
|
| 22 |
+
|
| 23 |
+
Then provide:
|
| 24 |
+
- An overall summary
|
| 25 |
+
- One motivational closing line
|
| 26 |
+
|
| 27 |
+
Writing:
|
| 28 |
+
{text}
|
| 29 |
+
"""
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="gpt-4",
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": f"You are a supportive writing coach responding in {language}."},
|
| 34 |
+
{"role": "user", "content": prompt}
|
| 35 |
+
],
|
| 36 |
+
temperature=0.7
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def improve_written_text(text, language):
|
| 42 |
+
prompt = f"""You are a writing coach. Rewrite this piece to improve structure, grammar, and clarity.
|
| 43 |
+
Make it more compelling while keeping the original meaning.
|
| 44 |
+
|
| 45 |
+
Text:
|
| 46 |
+
{text}
|
| 47 |
+
"""
|
| 48 |
+
response = client.chat.completions.create(
|
| 49 |
+
model="gpt-4",
|
| 50 |
+
messages=[
|
| 51 |
+
{"role": "system", "content": f"Reply in {language}. Provide only the improved version."},
|
| 52 |
+
{"role": "user", "content": prompt}
|
| 53 |
+
]
|
| 54 |
+
)
|
| 55 |
+
return response.choices[0].message.content
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def written_dashboard():
|
| 59 |
+
with gr.Column() as written_panel:
|
| 60 |
+
gr.Markdown("""
|
| 61 |
+
<div id="header" style="text-align: center;">
|
| 62 |
+
<img src="file/images/chatter_owl.png" width="120">
|
| 63 |
+
<h2>βοΈ Written Communication with <strong>Chatter the Owl</strong></h2>
|
| 64 |
+
<p>Paste your writing, select language, and Iβll help you sharpen your message.</p>
|
| 65 |
+
</div>
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
+
nickname_box = gr.Textbox(label="π€ Your Nickname", placeholder="Enter your nameβ¦")
|
| 69 |
+
language_dropdown = gr.Dropdown(label="π Select Language", choices=list(LANG_CODES.keys()), value="English")
|
| 70 |
+
font_size_slider = gr.Slider(12, 24, value=16, step=1, label="π Font Size")
|
| 71 |
+
contrast_toggle = gr.Checkbox(label="π High Contrast Text")
|
| 72 |
+
|
| 73 |
+
input_text = gr.Textbox(lines=7, label="π Your Writing")
|
| 74 |
+
feedback_box = gr.Textbox(label="π‘ Feedback", interactive=False)
|
| 75 |
+
improved_box = gr.Textbox(label="π― Improved Version", interactive=False)
|
| 76 |
+
audio_output = gr.Audio(label="π Chatter Speaks Feedback", type="filepath")
|
| 77 |
+
history_table = gr.Dataframe(headers=["π Timestamp", "π Language", "βοΈ Writing Preview", "π Feedback Preview"])
|
| 78 |
+
|
| 79 |
+
def tutor_written(text, language, nickname):
|
| 80 |
+
if not text.strip():
|
| 81 |
+
return "", "", None, []
|
| 82 |
+
|
| 83 |
+
feedback = generate_written_feedback(text, language)
|
| 84 |
+
improved = improve_written_text(text, language)
|
| 85 |
+
|
| 86 |
+
lang_code = LANG_CODES.get(language, "en")
|
| 87 |
+
tts = gTTS(feedback, lang=lang_code)
|
| 88 |
+
mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
|
| 89 |
+
tts.save(mp3_path)
|
| 90 |
+
|
| 91 |
+
save_to_db(nickname, text, feedback, language)
|
| 92 |
+
sessions = fetch_user_sessions(nickname)
|
| 93 |
+
session_table = [[s.timestamp, s.language, s.transcript[:40], s.feedback[:40]] for s in sessions]
|
| 94 |
+
|
| 95 |
+
return feedback, improved, mp3_path, session_table
|
| 96 |
+
|
| 97 |
+
input_text.change(
|
| 98 |
+
fn=tutor_written,
|
| 99 |
+
inputs=[input_text, language_dropdown, nickname_box],
|
| 100 |
+
outputs=[feedback_box, improved_box, audio_output, history_table]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
return written_panel
|