Spaces:
Runtime error
Runtime error
update
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
|
| 5 |
-
import spaces # Quan trọng để dùng ZeroGPU
|
| 6 |
|
| 7 |
-
# 1. Triệu hồi
|
| 8 |
model_id = "google/gemma-2b-it"
|
| 9 |
|
| 10 |
-
# Load Tokenizer và Model
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_id,
|
|
@@ -15,48 +13,32 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 15 |
device_map="auto",
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# 2.
|
| 19 |
-
@spaces.GPU
|
| 20 |
-
def
|
| 21 |
-
# Chuyển đổi
|
| 22 |
conversation = []
|
| 23 |
for user, assistant in history:
|
| 24 |
conversation.extend([{"role": "user", "content": user}, {"role": "model", "content": assistant}])
|
| 25 |
conversation.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
-
#
|
| 28 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
generate_kwargs = dict(
|
| 34 |
-
input_ids=input_ids,
|
| 35 |
-
streamer=streamer,
|
| 36 |
max_new_tokens=1024,
|
| 37 |
do_sample=True,
|
| 38 |
temperature=0.7,
|
| 39 |
-
top_k=50,
|
| 40 |
-
top_p=0.95,
|
| 41 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
t.start()
|
| 46 |
-
|
| 47 |
-
partial_message = ""
|
| 48 |
-
for new_token in streamer:
|
| 49 |
-
partial_message += new_token
|
| 50 |
-
yield partial_message
|
| 51 |
-
|
| 52 |
-
# 3. Khởi tạo giao diện Chatbot (và cũng là cổng API)
|
| 53 |
-
demo = gr.ChatInterface(
|
| 54 |
-
fn=chat_gemma,
|
| 55 |
-
title="Gemma Chatbot by Thầy Oáp 🚀",
|
| 56 |
-
description="Hỏi gì cũng đáp, lú đâu thsông đó!",
|
| 57 |
-
examples=["Giải thích định luật Newton bằng ngôn ngữ Gen Z", "Viết code Python tạo API"],
|
| 58 |
-
theme="soft"
|
| 59 |
-
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import spaces
|
|
|
|
| 5 |
|
| 6 |
+
# 1. Triệu hồi bộ não Gemma 2B
|
| 7 |
model_id = "google/gemma-2b-it"
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_id,
|
|
|
|
| 13 |
device_map="auto",
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# 2. Hàm xử lý API - Tập trung vào tốc độ và độ chính xác
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def api_chat(message, history):
|
| 19 |
+
# Chuyển đổi format cho Gemma
|
| 20 |
conversation = []
|
| 21 |
for user, assistant in history:
|
| 22 |
conversation.extend([{"role": "user", "content": user}, {"role": "model", "content": assistant}])
|
| 23 |
conversation.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
+
# Mã hóa đầu vào
|
| 26 |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 27 |
|
| 28 |
+
# Tạo phản hồi (không dùng Streamer để trả về 1 cục JSON cho dễ xử lý ở Web)
|
| 29 |
+
output_ids = model.generate(
|
| 30 |
+
input_ids,
|
|
|
|
|
|
|
|
|
|
| 31 |
max_new_tokens=1024,
|
| 32 |
do_sample=True,
|
| 33 |
temperature=0.7,
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
+
|
| 36 |
+
# Giải mã và trả kết quả
|
| 37 |
+
response = tokenizer.decode(output_ids[0][len(input_ids[0]):], skip_special_tokens=True)
|
| 38 |
+
return response
|
| 39 |
|
| 40 |
+
# 3. Khởi tạo interface kiểu "Cổng kết nối"
|
| 41 |
+
demo = gr.ChatInterface(fn=api_chat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
demo.launch()
|