Spaces:
Runtime error
Runtime error
Commit ·
a058ce7
1
Parent(s): 185a5dd
app.py
CHANGED
|
@@ -1,8 +1,20 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", torch_dtype=torch.float16, device_map="auto")
|
| 7 |
|
| 8 |
+
def respond(message):
|
| 9 |
+
prompt = f"<start_of_turn>user\n{message}<end_of_turn>\n<start_of_turn>model\n"
|
| 10 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 11 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 12 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 13 |
+
|
| 14 |
+
# 擷取模型回覆部分
|
| 15 |
+
if "<start_of_turn>model" in response:
|
| 16 |
+
response = response.split("<start_of_turn>model")[-1].strip()
|
| 17 |
+
return response
|
| 18 |
|
| 19 |
+
iface = gr.Interface(fn=respond, inputs="text", outputs="text")
|
| 20 |
+
iface.launch()
|