Spaces:
Sleeping
Sleeping
switch from Inference API to local model with tokenizer (openai-community/gpt2)
Browse files
app.py
CHANGED
@@ -1,57 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
import re
|
5 |
-
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
# torch_dtype=torch.bfloat16,
|
11 |
-
# attn_implementation="flash_attention_2",
|
12 |
-
#)
|
13 |
|
14 |
-
# Load API key from environment variables
|
15 |
-
#HF_API_TOKEN = os.getenv("HUG_TOKEN_READ2")
|
16 |
-
HF_INF_KEY = os.getenv("HF_INF_PROVIDERS_TOKEN")
|
17 |
-
#HF_INF_KEY = os.getenv("INF_NEBIUS")
|
18 |
-
|
19 |
-
# Hugging Face Inference API Client
|
20 |
-
#client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1", token=HF_API_TOKEN)
|
21 |
-
#client = InferenceClient(model="openGPT-X/Teuken-7B-instruct-commercial-v0.4", token=HF_API_TOKEN)
|
22 |
-
#client = InferenceClient(model=qwenModel, token=HF_API_TOKEN)
|
23 |
-
#client = InferenceClient(model="microsoft/Phi-4-mini-instruct", token=HF_API_TOKEN)
|
24 |
-
#client = InferenceClient(model="openai-community/gpt2", token=HF_API_TOKEN)
|
25 |
-
client = InferenceClient(
|
26 |
-
provider="nebius",
|
27 |
-
api_key=HF_INF_KEY
|
28 |
-
)
|
29 |
-
|
30 |
-
# Function to translate text into emojis
|
31 |
def text_to_emoji(text):
|
32 |
# remove special characters
|
33 |
text_cleaned = re.sub(r"[.,!?;:]", "", text)
|
34 |
-
|
35 |
-
prompt = f"Convert this sentence into an emoji-sequence which conveys a similar meaning and return only the emojis, no explanation:\n\n\"{text_cleaned}\""
|
36 |
-
#response = client.text_generation(prompt, max_new_tokens=50)
|
37 |
-
#return response
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
]
|
50 |
-
}
|
51 |
-
],
|
52 |
-
max_tokens=25
|
53 |
-
)
|
54 |
-
return completion.choices[0].message
|
55 |
|
56 |
# Gradio UI
|
57 |
iface = gr.Interface(
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import re
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
|
6 |
+
# Modell und Tokenizer laden
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
8 |
+
model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def text_to_emoji(text):
|
11 |
# remove special characters
|
12 |
text_cleaned = re.sub(r"[.,!?;:]", "", text)
|
13 |
+
prompt = f"Convert the following sentence into an emoji-sequence which conveys a similar meaning and return only the emojis, no explanation:\n\n\"{text_cleaned}\""
|
|
|
|
|
|
|
14 |
|
15 |
+
# Tokenisieren
|
16 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
17 |
+
|
18 |
+
# Antwort generieren
|
19 |
+
outputs = model.generate(**inputs, max_new_tokens=25, do_sample=True, temperature=0.7)
|
20 |
+
|
21 |
+
# Antwort decodieren
|
22 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
+
|
24 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Gradio UI
|
27 |
iface = gr.Interface(
|