Spaces:
Runtime error

File size: 2,686 Bytes
c771eee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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"
    }

    # More explicit system prompt for translation
    prompt = (
        f"You are a professional translator. ONLY return the translated version of the following message into {target_language}. "
        f"Do not add any notes, comments, or explanations. Keep the meaning and tone the same."
    )

    data = {
        "model": "llama3-8b-8192",
        "messages": [
            {"role": "system", "content": prompt},
            {"role": "user", "content": text}
        ]
    }

    try:
        response = requests.post(
            "https://api.groq.com/openai/v1/chat/completions",
            headers=headers,
            json=data,
            timeout=20
        )
        response.raise_for_status()
        content = response.json()['choices'][0]['message']['content'].strip()

        # Clean the result if there's extra explanation
        for unwanted in ["Translation:", "Translated text:", "Here is the"]:
            if content.lower().startswith(unwanted.lower()):
                content = content[len(unwanted):].strip(":").strip()

        return content

    except requests.exceptions.RequestException as e:
        return f"Error: {str(e)}"

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(\"\"\"\n    # AwaazHub\n    **Your Local Community Bulletin**\n    \n    Share local news, job openings, lost items, and more — auto-translated into regional languages to make sure everyone stays informed.\n    \"\"\")

    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()