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 "

No announcements yet.

" output = "" for a in reversed(announcements): color = color_map.get(a["category"], "#f0f0f0") image_tag = f"" if a["image_url"] else "" output += f"""

{a['category']}

Original: {a['original']}

{a['language']}: {a['translated']}

{image_tag}
""" 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()