Spaces:
Sleeping
Sleeping
switch to Gemini API
Browse files
app.py
CHANGED
@@ -1,42 +1,22 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import re
|
4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
-
import torch
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
|
10 |
-
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
# Eingabetext bereinigen (optional)
|
15 |
-
cleaned_text = re.sub(r"[.,!?;:]", "", input_text)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# Tokenisierung und Generation
|
22 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
23 |
-
outputs = model.generate(
|
24 |
-
**inputs,
|
25 |
-
max_new_tokens=10,
|
26 |
-
do_sample=True,
|
27 |
-
temperature=0.9,
|
28 |
-
top_k=50,
|
29 |
-
pad_token_id=tokenizer.eos_token_id # Prevents warning
|
30 |
-
)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
# Nur den generierten Teil nach dem letzten "→"
|
36 |
-
emoji_part = generated_text.split("→")[-1].strip().split("\n")[0]
|
37 |
-
|
38 |
-
return emoji_part
|
39 |
-
|
40 |
# Gradio UI
|
41 |
iface = gr.Interface(
|
42 |
fn=text_to_emoji,
|
@@ -46,4 +26,4 @@ iface = gr.Interface(
|
|
46 |
description="Enter a sentence, and the AI will transform it into an emoji-version 🥳"
|
47 |
)
|
48 |
|
49 |
-
iface.launch()
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
import gradio as gr
|
3 |
+
import os
|
4 |
import re
|
|
|
|
|
5 |
|
6 |
+
# Load Google AI API
|
7 |
+
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
|
|
|
|
8 |
|
9 |
+
# Specify model
|
10 |
+
model = genai.GenerativeModel('gemini-2.5-pro-exp-03-25')
|
|
|
|
|
11 |
|
12 |
+
# Function to translate text into emojis using OpenAI API
|
13 |
+
def text_to_emoji(text):
|
14 |
+
text_cleaned = re.sub(r"[.,!?;:]", "", text)
|
15 |
+
prompt = f"Convert this sentence into an emoji-sequence of the same meaning and return only the emojis, no explanation:\n\n\"{text_cleaned}\""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
response = model.generate_content(prompt)
|
18 |
+
return response.text.strip()
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
# Gradio UI
|
21 |
iface = gr.Interface(
|
22 |
fn=text_to_emoji,
|
|
|
26 |
description="Enter a sentence, and the AI will transform it into an emoji-version 🥳"
|
27 |
)
|
28 |
|
29 |
+
iface.launch(debug=True)
|