Spaces:
Runtime error
Runtime error
Commit ·
0f20fb4
1
Parent(s): 88efe82
test deepseek
Browse files- app.py +31 -9
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -45,10 +45,38 @@ def respond(
|
|
| 45 |
print("\n")
|
| 46 |
yield response #chat reply
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
chat = gr.ChatInterface(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
additional_inputs=[
|
| 53 |
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
|
| 54 |
gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
|
|
@@ -57,12 +85,6 @@ chat = gr.ChatInterface(
|
|
| 57 |
# gr.Image(type="pil", label="Attach an image (optional)"),
|
| 58 |
# gr.File(label="Upload a file (optional)"),
|
| 59 |
],
|
| 60 |
-
examples=[
|
| 61 |
-
# Mỗi item: [message, system_message, max_tokens, temperature, top_p]
|
| 62 |
-
"Xin chào!",
|
| 63 |
-
"Tóm tắt đoạn văn sau…",
|
| 64 |
-
"Viết thơ 4 câu về hoa sen",
|
| 65 |
-
],
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 45 |
print("\n")
|
| 46 |
yield response #chat reply
|
| 47 |
|
| 48 |
+
import torch
|
| 49 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 50 |
+
def deepseek(
|
| 51 |
+
message,
|
| 52 |
+
history: list[tuple[str, str]],
|
| 53 |
+
system_message,
|
| 54 |
+
max_tokens,
|
| 55 |
+
temperature,
|
| 56 |
+
top_p,
|
| 57 |
+
image_uploaded,
|
| 58 |
+
file_uploaded):
|
| 59 |
+
model_name = "deepseek-ai/deepseek-math-7b-instruct"
|
| 60 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 61 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
|
| 62 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 63 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 64 |
+
|
| 65 |
+
messages = [
|
| 66 |
+
{"role": "user", "content": "what is the integral of x^2 from 0 to 2?\nPlease reason step by step, and put your final answer within \\boxed{}."}
|
| 67 |
+
]
|
| 68 |
+
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 69 |
+
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
| 70 |
+
print(outputs)
|
| 71 |
+
print("\n")
|
| 72 |
+
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
| 73 |
+
print(result)
|
| 74 |
+
return result
|
| 75 |
+
|
| 76 |
chat = gr.ChatInterface(
|
| 77 |
+
deepseek, #chat
|
| 78 |
+
title="Trợ lý Học Tập AI",
|
| 79 |
+
description="Nhập câu hỏi của bạn về Toán, Lý, Hóa, Văn… và nhận giải đáp chi tiết ngay lập tức!",
|
| 80 |
additional_inputs=[
|
| 81 |
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
|
| 82 |
gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
|
|
|
|
| 85 |
# gr.Image(type="pil", label="Attach an image (optional)"),
|
| 86 |
# gr.File(label="Upload a file (optional)"),
|
| 87 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
-
openai
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
openai
|
| 3 |
+
torch
|
| 4 |
+
transformers
|