Spaces:
Runtime error
Runtime error
Commit ·
f581fc4
1
Parent(s): 071c4e7
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
# Load the model and tokenizer
|
|
@@ -11,11 +12,20 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
# Define a simple chat function
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Gradio interface
|
| 21 |
with gr.Blocks() as interface:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
# Load the model and tokenizer
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# Define a simple chat function
|
| 15 |
+
|
| 16 |
+
def chat(input_text, max_tokens):
|
| 17 |
+
try:
|
| 18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
| 20 |
+
model.to(device)
|
| 21 |
+
|
| 22 |
+
outputs = model.generate(**inputs, max_new_tokens=max_tokens, do_sample=True)
|
| 23 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
return response
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {str(e)}"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
|
| 30 |
# Gradio interface
|
| 31 |
with gr.Blocks() as interface:
|