Spaces:
Runtime error
Runtime error
Thầy Oáp độ code Gemma
Browse files- API_chatbot +1 -0
- app.py +62 -0
- requirements.txt +5 -0
API_chatbot
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Subproject commit 72d421c576ee51b796fd88f2f119fb64cd8f97c2
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
import spaces # Quan trọng để dùng ZeroGPU
|
| 6 |
+
|
| 7 |
+
# 1. Triệu hồi "linh vật" Gemma (Bản 2b-it là hợp lý nhất cho Space)
|
| 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,
|
| 14 |
+
torch_dtype=torch.bfloat16,
|
| 15 |
+
device_map="auto",
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# 2. Tuyệt chiêu xử lý tin nhắn với ZeroGPU
|
| 19 |
+
@spaces.GPU # Cấp quyền dùng GPU tạm thời cho hàm này
|
| 20 |
+
def chat_gemma(message, history):
|
| 21 |
+
# Chuyển đổi lịch sử chat sang định dạng Gemma hiểu được
|
| 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 |
+
# Tokenize đầu vào
|
| 28 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 29 |
+
|
| 30 |
+
# Thiết lập Streamer để chữ nhảy ra từng chữ cho "ngầu"
|
| 31 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 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 |
+
# Chạy luồng phụ để generate văn bản
|
| 44 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
accelerate
|
| 5 |
+
spaces
|