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