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"Original: {a['original']}
{a['language']}: {a['translated']}
{image_tag}