# app.py import gradio as gr import requests import os # Set your GROQ API key here groq_api_key = os.getenv("GROQ_API_KEY") or "your-groq-api-key" def translate_text(text, target_language): headers = { "Authorization": f"Bearer {groq_api_key}", "Content-Type": "application/json" } data = { "model": "llama3-8b-8192", "messages": [ {"role": "system", "content": f"Translate the following message to {target_language}."}, {"role": "user", "content": text} ] } response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data) if response.status_code == 200: return response.json()['choices'][0]['message']['content'].strip() else: return f"Error: {response.status_code} - {response.text}" def handle_submission(post_text, category, language): translated = translate_text(post_text, language) return f"**Category:** {category}\n\n**Original:** {post_text}\n\n**Translated to {language}:** {translated}" with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown(""" # Community Hub Share local announcements, job posts, events, or lost & found items. Your message will be automatically translated! """) with gr.Row(): post_text = gr.Textbox(label="Your Message", lines=4, placeholder="Write your announcement here...") with gr.Row(): category = gr.Dropdown( choices=["Announcements", "Events", "Job Postings", "Lost & Found", "Community Initiatives"], label="Select Category" ) language = gr.Dropdown( choices=["Urdu", "Punjabi", "Sindhi", "Pashto", "Balochi", "Saraiki"], label="Translate To" ) submit_btn = gr.Button("Post") output = gr.Markdown() submit_btn.click(fn=handle_submission, inputs=[post_text, category, language], outputs=output) if __name__ == "__main__": demo.launch()