Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,41 @@
|
|
| 1 |
-
# ๐ง SoberUp: Drunk-to-Sober Text Translator (Gradio Edition)
|
| 2 |
-
|
| 3 |
-
## Install Gemini API & Gradio
|
| 4 |
-
```bash
|
| 5 |
-
pip install -q -U google-genai gradio
|
| 6 |
-
```
|
| 7 |
-
|
| 8 |
-
---
|
| 9 |
-
|
| 10 |
-
## ๐ฆ `app.py`: The Gradio Interface
|
| 11 |
-
```python
|
| 12 |
import gradio as gr
|
| 13 |
from google import genai
|
| 14 |
import os
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
model = client.models.get("models/gemini-2.0-flash")
|
| 19 |
|
|
|
|
| 20 |
def sober_up(drunk_text):
|
| 21 |
if not drunk_text.strip():
|
| 22 |
return "Please enter a message."
|
|
|
|
| 23 |
prompt = f"Translate this drunk message into a sober, coherent version: '{drunk_text}'"
|
|
|
|
| 24 |
try:
|
| 25 |
response = model.generate_content(contents=prompt)
|
| 26 |
return response.text
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error: {e}"
|
| 29 |
|
| 30 |
-
# Gradio
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=sober_up,
|
| 33 |
-
inputs=gr.Textbox(lines=4, placeholder="
|
| 34 |
outputs="text",
|
| 35 |
title="๐บ SoberUp Translator",
|
| 36 |
-
description="
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
---
|
| 43 |
-
|
| 44 |
-
## ๐งช Example Prompt:
|
| 45 |
-
```text
|
| 46 |
-
oh mannn i just met a guy at tha taco bell drivethru named jason and weโre startin a band called laser lizards ๐๐ธ๐ฎ
|
| 47 |
-
```
|
| 48 |
-
|
| 49 |
-
**Output:**
|
| 50 |
-
```text
|
| 51 |
-
I just met a guy named Jason at Taco Bell. We're starting a band called Laser Lizards.
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
---
|
| 55 |
-
|
| 56 |
-
## ๐ Security Tips
|
| 57 |
-
- Use `os.getenv("GEMINI_API_KEY")` from Hugging Face secrets panel
|
| 58 |
-
- Optionally add profanity filtering or moderation layer
|
| 59 |
-
|
| 60 |
-
Want it hosted on Hugging Face or deployed as a mobile app? Just say the word: `> DEPLOY TO CLOUD` or `> MAKE MOBILE VERSION`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from google import genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Get API key from environment variable (set this in Hugging Face Secrets)
|
| 6 |
+
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
+
|
| 8 |
+
if not API_KEY:
|
| 9 |
+
raise EnvironmentError("Missing GEMINI_API_KEY. Set it in Hugging Face Spaces โ Secrets.")
|
| 10 |
+
|
| 11 |
+
# Initialize the Gemini client
|
| 12 |
+
client = genai.Client(api_key=API_KEY)
|
| 13 |
+
|
| 14 |
+
# Load Gemini model
|
| 15 |
model = client.models.get("models/gemini-2.0-flash")
|
| 16 |
|
| 17 |
+
# Translation function
|
| 18 |
def sober_up(drunk_text):
|
| 19 |
if not drunk_text.strip():
|
| 20 |
return "Please enter a message."
|
| 21 |
+
|
| 22 |
prompt = f"Translate this drunk message into a sober, coherent version: '{drunk_text}'"
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
response = model.generate_content(contents=prompt)
|
| 26 |
return response.text
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error: {e}"
|
| 29 |
|
| 30 |
+
# Gradio Interface
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=sober_up,
|
| 33 |
+
inputs=gr.Textbox(lines=4, placeholder="Paste your chaotic, drunk text here..."),
|
| 34 |
outputs="text",
|
| 35 |
title="๐บ SoberUp Translator",
|
| 36 |
+
description="AI-powered translator for decoding drunk messages into sober, coherent statements using Gemini Flash."
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Launch app
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|