Download written_module.py from ankban/CommTutor: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/written_module.py
- Command line
-
hf download hf://spaces/ankban/CommTutor@8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/written_module.py
-
curl -L -o written_module.py https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/written_module.py
4.31 kB
| 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 | |