Download app.py from Ai1996/AwazHub: direct link, hf CLI and curl.
- Browser
- Download file 4.29 kB
-
https://huggingface.co/spaces/Ai1996/AwazHub/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ai1996/AwazHub/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ai1996/AwazHub/resolve/main/app.py
4.29 kB
| import gradio as gr | |
| import requests | |
| import os | |
| import shutil | |
| from uuid import uuid4 | |
| # Create static directory if not exists | |
| STATIC_DIR = "static" | |
| os.makedirs(STATIC_DIR, exist_ok=True) | |
| # Store announcements | |
| announcements = [] | |
| # Language options | |
| LANGUAGES = { | |
| "English": "English", | |
| "Urdu": "Urdu", | |
| "Punjabi": "Punjabi", | |
| "Sindhi": "Sindhi" | |
| } | |
| # Color mapping | |
| color_map = { | |
| "Events": "#ffd700", | |
| "Job Postings": "#90ee90", | |
| "Lost & Found": "#add8e6", | |
| "Community Initiatives": "#ffa07a", | |
| "General Announcement": "#dda0dd" | |
| } | |
| # GROQ API credentials | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| # Translation function | |
| def translate(text, target_lang): | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| prompt = f"Translate this exactly into {target_lang} without adding any explanation or label:\n\n{text}" | |
| data = { | |
| "model": "llama3-8b-8192", | |
| "messages": [ | |
| {"role": "system", "content": "You are a professional translator. Just return the translation only."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| "temperature": 0.3 | |
| } | |
| response = requests.post(GROQ_API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()['choices'][0]['message']['content'].strip() | |
| else: | |
| return "Translation error." | |
| # Post announcement | |
| def post_announcement(category, text, language, image): | |
| translated = translate(text, language) | |
| image_url = "" | |
| if category == "Lost & Found" and image: | |
| ext = os.path.splitext(image)[-1] | |
| filename = f"{uuid4().hex}{ext}" | |
| filepath = os.path.join(STATIC_DIR, filename) | |
| shutil.copy(image, filepath) | |
| image_url = f"file={filepath}" # Used by Gradio in HTML | |
| announcements.append({ | |
| "category": category, | |
| "original": text, | |
| "translated": translated, | |
| "language": language, | |
| "image_url": image_url | |
| }) | |
| return "β Announcement posted! You can view it on the main board." | |
| # Render announcements in HTML | |
| def render_announcements(): | |
| if not announcements: | |
| return "<p>No announcements yet.</p>" | |
| output = "" | |
| for a in reversed(announcements): | |
| color = color_map.get(a["category"], "#f0f0f0") | |
| image_tag = f"<img src='{a['image_url']}' style='max-width:300px; margin-top:10px; border-radius:10px;' />" if a["image_url"] else "" | |
| output += f""" | |
| <div style='border:1px solid #444; padding:15px; border-radius:12px; margin:15px 0; background-color:{color}; font-family:sans-serif'> | |
| <h3><b>{a['category']}</b></h3> | |
| <p><b>Original:</b> {a['original']}</p> | |
| <p><b>{a['language']}:</b> {a['translated']}</p> | |
| {image_tag} | |
| </div> | |
| """ | |
| return output | |
| # Gradio UI | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π **AwazHub β Multilingual Community Bulletin Board**") | |
| with gr.Tabs(): | |
| with gr.TabItem("π’ View Announcements"): | |
| announcement_output = gr.HTML(render_announcements()) | |
| refresh_btn = gr.Button("π Refresh Announcements") | |
| refresh_btn.click(fn=render_announcements, outputs=announcement_output) | |
| with gr.TabItem("π Post Announcement"): | |
| gr.Markdown("### π Submit a New Announcement") | |
| with gr.Row(): | |
| category = gr.Dropdown( | |
| ["General Announcement", "Events", "Job Postings", "Lost & Found", "Community Initiatives"], | |
| label="Select Category", value="General Announcement" | |
| ) | |
| language = gr.Dropdown(list(LANGUAGES.values()), label="Translate To") | |
| text = gr.Textbox(lines=4, label="Your Announcement") | |
| image = gr.Image(type="filepath", label="Upload Image (only for Lost & Found)") | |
| post_btn = gr.Button("π€ Post") | |
| status = gr.Textbox(label="", interactive=False) | |
| post_btn.click(fn=post_announcement, | |
| inputs=[category, text, language, image], | |
| outputs=status) | |
| demo.launch() |