Spaces:
Runtime error
Runtime error
Commit ·
b73f11f
1
Parent(s): 2e1418a
v6
Browse files
app.py
CHANGED
|
@@ -1,20 +1,24 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
|
| 6 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 20 |
-
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 2 |
+
from huggingface_hub import login
|
| 3 |
+
import os
|
| 4 |
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# 登入 Hugging Face(換成你自己的 token)
|
| 8 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
+
login(hf_token)
|
| 10 |
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
"google/gemma-2b-it",
|
| 14 |
+
torch_dtype=torch.float32
|
| 15 |
+
).to("cpu")
|
| 16 |
+
|
| 17 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 18 |
|
| 19 |
+
def generate(text):
|
| 20 |
+
result = pipe(text, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 21 |
+
return result[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
demo = gr.Interface(fn=generate, inputs="text", outputs="text")
|
| 24 |
+
demo.launch()
|