Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,19 @@ import gradio as gr
|
|
| 2 |
from google import genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
|
| 8 |
if not API_KEY:
|
| 9 |
-
raise EnvironmentError("GEMINI_API_KEY is not set. Add it
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
genai.
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
model =
|
| 16 |
|
| 17 |
-
# Define
|
| 18 |
def sober_up(drunk_text):
|
| 19 |
if not drunk_text.strip():
|
| 20 |
return "Please enter a message."
|
|
@@ -22,18 +22,21 @@ def sober_up(drunk_text):
|
|
| 22 |
prompt = f"Translate this drunk message into a sober, coherent version: '{drunk_text}'"
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
response =
|
|
|
|
|
|
|
|
|
|
| 26 |
return response.text
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error: {e}"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=sober_up,
|
| 33 |
inputs=gr.Textbox(lines=4, placeholder="Paste your chaotic, drunk message here..."),
|
| 34 |
outputs="text",
|
| 35 |
title="🍺 SoberUp Translator",
|
| 36 |
-
description="Turn drunk
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from google import genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Get API key from Hugging Face Secrets
|
| 6 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
|
| 8 |
if not API_KEY:
|
| 9 |
+
raise EnvironmentError("GEMINI_API_KEY is not set. Add it under 'Secrets' in Hugging Face Spaces.")
|
| 10 |
|
| 11 |
+
# ✅ Proper Gemini initialization
|
| 12 |
+
client = genai.Client(api_key=API_KEY)
|
| 13 |
|
| 14 |
+
# ✅ Correct way to load the Gemini 2.0 Flash model
|
| 15 |
+
model = client.models.generate_content
|
| 16 |
|
| 17 |
+
# Define translation logic
|
| 18 |
def sober_up(drunk_text):
|
| 19 |
if not drunk_text.strip():
|
| 20 |
return "Please enter a message."
|
|
|
|
| 22 |
prompt = f"Translate this drunk message into a sober, coherent version: '{drunk_text}'"
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
response = client.models.generate_content(
|
| 26 |
+
model="gemini-2.0-flash",
|
| 27 |
+
contents=prompt
|
| 28 |
+
)
|
| 29 |
return response.text
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error: {e}"
|
| 32 |
|
| 33 |
+
# Gradio UI
|
| 34 |
demo = gr.Interface(
|
| 35 |
fn=sober_up,
|
| 36 |
inputs=gr.Textbox(lines=4, placeholder="Paste your chaotic, drunk message here..."),
|
| 37 |
outputs="text",
|
| 38 |
title="🍺 SoberUp Translator",
|
| 39 |
+
description="Turn drunk ramblings into sober brilliance using Gemini 2.0 Flash."
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|