Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
os.environ["HF_HOME"] = "/tmp/.cache"
|
| 9 |
-
os.environ["HF_DATASETS_CACHE"] = "/tmp/.cache"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 16 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 17 |
-
|
| 18 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
-
model = model.to(device)
|
| 20 |
-
|
| 21 |
-
# Funcția chatbot
|
| 22 |
-
def chat_fn(question):
|
| 23 |
if not question.strip():
|
| 24 |
return "Te rog scrie o întrebare."
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Gradio UI
|
| 32 |
iface = gr.Interface(
|
| 33 |
-
fn=
|
| 34 |
inputs=gr.Textbox(lines=2, placeholder="Întreabă ceva..."),
|
| 35 |
outputs="text",
|
| 36 |
-
title="Chatbot
|
| 37 |
-
description="Chatbot
|
| 38 |
)
|
| 39 |
|
| 40 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Alege un model public HF (gratuit)
|
| 6 |
+
MODEL_ID = "google/flan-t5-small" # poți folosi orice model HF
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 9 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 10 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}
|
| 11 |
|
| 12 |
+
def ask_ai(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if not question.strip():
|
| 14 |
return "Te rog scrie o întrebare."
|
| 15 |
|
| 16 |
+
payload = {
|
| 17 |
+
"inputs": question,
|
| 18 |
+
"parameters": {"max_new_tokens": 150, "return_full_text": False}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
r = requests.post(HF_API_URL, headers=HEADERS, json=payload, timeout=30)
|
| 23 |
+
r.raise_for_status()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Eroare la apel HF Inference API: {str(e)}"
|
| 26 |
+
|
| 27 |
+
res = r.json()
|
| 28 |
+
# modelele HF returnează listă cu generated_text
|
| 29 |
+
if isinstance(res, list) and "generated_text" in res[0]:
|
| 30 |
+
return res[0]["generated_text"]
|
| 31 |
+
return str(res)
|
| 32 |
|
|
|
|
| 33 |
iface = gr.Interface(
|
| 34 |
+
fn=ask_ai,
|
| 35 |
inputs=gr.Textbox(lines=2, placeholder="Întreabă ceva..."),
|
| 36 |
outputs="text",
|
| 37 |
+
title="Chatbot HF API",
|
| 38 |
+
description="Chatbot funcțional fără descărcarea modelelor, rulând HF Inference API."
|
| 39 |
)
|
| 40 |
|
| 41 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|