Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Setup Groq client
|
| 6 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
client = Groq(api_key=groq_api_key)
|
| 8 |
+
|
| 9 |
+
lang_options = ["English", "Urdu", "Spanish", "French", "Arabic", "Chinese", "German"]
|
| 10 |
+
|
| 11 |
+
def translate_with_groq(text, src_lang, tgt_lang):
|
| 12 |
+
if src_lang == tgt_lang:
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
prompt = f"Translate the following text from {src_lang} to {tgt_lang}:\n\n\"{text}\""
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
chat_completion = client.chat.completions.create(
|
| 19 |
+
messages=[{"role": "user", "content": prompt}],
|
| 20 |
+
model="gemma-7b-it",
|
| 21 |
+
temperature=0.4
|
| 22 |
+
)
|
| 23 |
+
translated = chat_completion.choices[0].message.content.strip()
|
| 24 |
+
return translated
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=translate_with_groq,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(label="Enter text"),
|
| 33 |
+
gr.Dropdown(choices=lang_options, label="Source Language", value="English"),
|
| 34 |
+
gr.Dropdown(choices=lang_options, label="Target Language", value="Urdu")
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.Textbox(label="Translated Text"),
|
| 37 |
+
title="🌍 AI Translator with Groq",
|
| 38 |
+
description="Translate between multiple languages using Groq LLM (Gemma-7B-IT)."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|