Spaces:
Sleeping
Sleeping
Download app.py from Ai1996/Farming_App: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/Ai1996/Farming_App/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ai1996/Farming_App/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ai1996/Farming_App/resolve/main/app.py
1.98 kB
| # 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() |