ai01firebird commited on
Commit
783a256
·
verified ·
1 Parent(s): de82dd1

switch to Gemini API

Browse files
Files changed (1) hide show
  1. app.py +13 -33
app.py CHANGED
@@ -1,42 +1,22 @@
 
1
  import gradio as gr
2
- from prompt_prefix import prompt_prefix
3
  import re
4
- from transformers import AutoTokenizer, AutoModelForCausalLM
5
- import torch
6
 
7
- # Modell und Tokenizer laden
8
- # distilgpt2 is only 80MB -> no inference model, thus add prompt_prefix or train
9
- tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
10
- model = AutoModelForCausalLM.from_pretrained("distilgpt2")
11
 
12
- # conversion method
13
- def text_to_emoji(input_text):
14
- # Eingabetext bereinigen (optional)
15
- cleaned_text = re.sub(r"[.,!?;:]", "", input_text)
16
 
17
- # Pattern-based prompt with data from prompt_prefix
18
- #prompt = "(\n" + "".join(f'"{line}\\n"\n' for line in prompt_prefix) + f"\"{input_text} →\"" + ")"
19
- prompt = "(\n" + "".join(f'{line}\n' for line in prompt_prefix) + f"{input_text}"
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
- # Decodieren
33
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
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)