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("""

✍️ Written Communication with Chatter the Owl

Paste your writing or upload a file, select language, and I’ll help you sharpen your message.

""") 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