Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from google import genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
raise ValueError("Missing GEMINI_API_KEY. Make sure it is set in Hugging Face secrets.")
|
| 9 |
-
|
| 10 |
-
# Configure Gemini client
|
| 11 |
-
genai.configure(api_key=API_KEY)
|
| 12 |
-
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
| 13 |
|
| 14 |
def sober_up(drunk_text):
|
| 15 |
if not drunk_text.strip():
|
| 16 |
return "Please enter a message."
|
| 17 |
prompt = f"Translate this drunk message into a sober, coherent version: '{drunk_text}'"
|
| 18 |
try:
|
| 19 |
-
response = model.generate_content(prompt)
|
| 20 |
return response.text
|
| 21 |
except Exception as e:
|
| 22 |
return f"Error: {e}"
|
| 23 |
|
|
|
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=sober_up,
|
| 26 |
-
inputs=gr.Textbox(lines=4, placeholder="
|
| 27 |
outputs="text",
|
| 28 |
title="๐บ SoberUp Translator",
|
| 29 |
-
description="
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Correct Gemini API setup
|
| 17 |
+
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
|
| 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 UI
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=sober_up,
|
| 33 |
+
inputs=gr.Textbox(lines=4, placeholder="Type or paste your drunk text here..."),
|
| 34 |
outputs="text",
|
| 35 |
title="๐บ SoberUp Translator",
|
| 36 |
+
description="Paste your chaotic, drunk ramblings and get a polished, sober version."
|
| 37 |
)
|
| 38 |
|
| 39 |
demo.launch()
|
| 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`.
|