Download src/ui.py from hoom4n/SentiNet: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/hoom4n/SentiNet/resolve/main/src/ui.py
- Command line
-
hf download hf://spaces/hoom4n/SentiNet/src/ui.py
-
curl -L -o ui.py https://huggingface.co/spaces/hoom4n/SentiNet/resolve/main/src/ui.py
2.64 kB
| import os | |
| import gradio as gr | |
| def read_file(path: str, default_content: str = "") -> str: | |
| """ | |
| Ensure file exists (with default_content if missing) and return its contents. | |
| """ | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| if not os.path.exists(path): | |
| with open(path, "w", encoding="utf-8") as f: | |
| f.write(default_content) | |
| with open(path, "r", encoding="utf-8") as f: | |
| return f.read() | |
| def build_demo( | |
| generation_fn, | |
| english_title: str, | |
| persian_title: str, | |
| assets_dir: str = "assets", | |
| app_title: str = "SentiNet Demo" | |
| ): | |
| md_dir = os.path.join(assets_dir, "markdown") | |
| css_dir = os.path.join(assets_dir, "css") | |
| english_md = os.path.join(md_dir, "english_summary.md") | |
| persian_md = os.path.join(md_dir, "persian_summary.md") | |
| english_summary = read_file(english_md) | |
| persian_summary = read_file(persian_md) | |
| css_file = os.path.join(css_dir, "custom.css") | |
| css = read_file(css_file, "/* Custom CSS overrides */\n") | |
| with gr.Blocks(css=css, title=app_title) as demo: | |
| title_md = gr.Markdown(english_title, elem_id="title") | |
| with gr.Row(): | |
| english_btn = gr.Button("English") | |
| persian_btn = gr.Button("فارسی (Persian)") | |
| summary_md = gr.Markdown(english_summary, elem_id="summary") | |
| # generation panel | |
| with gr.Row(variant="panel"): | |
| with gr.Column(scale=1, variant="panel"): | |
| text_inp = gr.Textbox( | |
| label="Enter a sentence", | |
| placeholder="Type your movie review here..." | |
| ) | |
| generate_btn = gr.Button("🔍 Classify Sentiment", variant="primary") | |
| with gr.Column(scale=1, variant="panel"): | |
| sentiment_label = gr.Label(label="Prediction") | |
| confidence_out = gr.Number(label="Confidence Score") | |
| # events | |
| generate_btn.click( | |
| generation_fn, | |
| inputs=[text_inp], | |
| outputs=[sentiment_label, confidence_out] | |
| ) | |
| def set_english(): | |
| return ( | |
| gr.update(value=english_title, elem_classes=[]), | |
| gr.update(value=english_summary, elem_classes=[]), | |
| ) | |
| def set_persian(): | |
| return ( | |
| gr.update(value=persian_title, elem_classes=["persian"]), | |
| gr.update(value=persian_summary, elem_classes=["persian"]), | |
| ) | |
| english_btn.click(set_english, outputs=[title_md, summary_md]) | |
| persian_btn.click(set_persian, outputs=[title_md, summary_md]) | |
| return demo | |