Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
# STEP 2: Import required libraries
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -19,12 +18,10 @@ def translate_text(text, target_language):
|
|
| 19 |
],
|
| 20 |
model="llama3-8b-8192"
|
| 21 |
)
|
| 22 |
-
|
| 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=[
|
|
@@ -33,8 +30,8 @@ iface = gr.Interface(
|
|
| 33 |
],
|
| 34 |
outputs=gr.Textbox(label="Translated Text"),
|
| 35 |
title="🌍 Translation App",
|
| 36 |
-
description="Translate English text into any
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Use your API key from environment variable
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
|
|
|
|
|
|
|
| 8 |
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
|
|
|
|
| 10 |
def translate_text(text, target_language):
|
| 11 |
prompt = f"Translate the following English sentence into {target_language}:\n\n\"{text}\""
|
| 12 |
|
|
|
|
| 18 |
],
|
| 19 |
model="llama3-8b-8192"
|
| 20 |
)
|
| 21 |
+
return response.choices[0].message.content.strip()
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error: {str(e)}"
|
| 24 |
|
|
|
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=translate_text,
|
| 27 |
inputs=[
|
|
|
|
| 30 |
],
|
| 31 |
outputs=gr.Textbox(label="Translated Text"),
|
| 32 |
title="🌍 Translation App",
|
| 33 |
+
description="Translate English text into any language using Groq LLM"
|
| 34 |
)
|
| 35 |
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|