Spaces:
Sleeping
Sleeping
Api only access
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Load model + tokenizer
|
| 6 |
model_id = "MahiH/dialogpt-finetuned-chatbot"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
|
@@ -10,26 +9,21 @@ model.eval()
|
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
model.to(device)
|
| 12 |
|
| 13 |
-
# Inference function
|
| 14 |
def chat(prompt):
|
| 15 |
input_text = f"Human: {prompt}\nAssistant: "
|
| 16 |
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 17 |
-
|
| 18 |
with torch.no_grad():
|
| 19 |
output_ids = model.generate(
|
| 20 |
input_ids,
|
| 21 |
max_new_tokens=100,
|
| 22 |
do_sample=True,
|
| 23 |
-
top_k=50,
|
| 24 |
top_p=0.95,
|
| 25 |
temperature=0.8,
|
| 26 |
pad_token_id=tokenizer.eos_token_id
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return response.split("Assistant:")[-1].strip()
|
| 31 |
-
|
| 32 |
-
# Set up Gradio app (no UI, just API)
|
| 33 |
-
app = gr.Interface(fn=chat, inputs=gr.Text(), outputs=gr.Text())
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
model_id = "MahiH/dialogpt-finetuned-chatbot"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
|
|
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
model.to(device)
|
| 11 |
|
|
|
|
| 12 |
def chat(prompt):
|
| 13 |
input_text = f"Human: {prompt}\nAssistant: "
|
| 14 |
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
|
|
|
| 15 |
with torch.no_grad():
|
| 16 |
output_ids = model.generate(
|
| 17 |
input_ids,
|
| 18 |
max_new_tokens=100,
|
| 19 |
do_sample=True,
|
|
|
|
| 20 |
top_p=0.95,
|
| 21 |
temperature=0.8,
|
| 22 |
pad_token_id=tokenizer.eos_token_id
|
| 23 |
)
|
| 24 |
+
decoded = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 25 |
+
return decoded.split("Assistant:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Setup API-only interface (UI will still be generated but ignored)
|
| 28 |
+
demo = gr.Interface(fn=chat, inputs="text", outputs="text")
|
| 29 |
+
demo.launch() # DO NOT use share=True
|