File size: 4,285 Bytes
99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 99813ff d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 d824bf8 34399c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 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() |