Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# STEP 2: Import required libraries
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from google.colab import userdata
|
| 5 |
+
|
| 6 |
+
# STEP 3: Set your Groq API key
|
| 7 |
+
GROQ_API_KEY = userdata.get('GROQ_API_KEY') # ⬅️ Replace with your actual Groq API Key
|
| 8 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
+
|
| 10 |
+
# STEP 4: Function to translate text using Groq API
|
| 11 |
+
def translate_text(text, target_language):
|
| 12 |
+
prompt = f"Translate the following English sentence into {target_language}:\n\n\"{text}\""
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
response = client.chat.completions.create(
|
| 16 |
+
messages=[
|
| 17 |
+
{"role": "system", "content": "You are a multilingual translator."},
|
| 18 |
+
{"role": "user", "content": prompt}
|
| 19 |
+
],
|
| 20 |
+
model="llama3-8b-8192"
|
| 21 |
+
)
|
| 22 |
+
translated = response.choices[0].message.content.strip()
|
| 23 |
+
return translated
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {str(e)}"
|
| 26 |
+
|
| 27 |
+
# STEP 5: Gradio Interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=translate_text,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(lines=2, label="Enter English text"),
|
| 32 |
+
gr.Textbox(label="Target Language (e.g., Spanish, French, Urdu)")
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Textbox(label="Translated Text"),
|
| 35 |
+
title="🌍 Translation App",
|
| 36 |
+
description="Translate English text into any desired language using Groq LLM."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# STEP 6: Launch the app
|
| 40 |
+
iface.launch()
|