Spaces:
Sleeping
Sleeping
Ismail-Alif
commited on
Commit
·
6abf429
1
Parent(s):
3b2f6bf
model changes
Browse files- app.py +37 -23
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,32 +1,46 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
-
# Load
|
6 |
-
model_name = "
|
7 |
-
tokenizer =
|
8 |
-
model =
|
|
|
|
|
|
|
|
|
9 |
model.eval()
|
10 |
|
11 |
# Chat function
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
outputs = model.generate(
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
history.append((message,
|
23 |
-
return
|
24 |
|
25 |
-
# Gradio
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
# Launch
|
31 |
if __name__ == "__main__":
|
32 |
-
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import gradio as gr
|
4 |
|
5 |
+
# Load the DeepSeek model and tokenizer
|
6 |
+
model_name = "deepseek-ai/deepseek-llm-7b-base"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype=torch.float16,
|
11 |
+
device_map="auto"
|
12 |
+
)
|
13 |
model.eval()
|
14 |
|
15 |
# Chat function
|
16 |
+
def chat(message, history=[]):
|
17 |
+
history_text = "".join([f"User: {u}\nAssistant: {a}\n" for u, a in history])
|
18 |
+
prompt = history_text + f"User: {message}\nAssistant:"
|
19 |
+
|
20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
21 |
+
outputs = model.generate(
|
22 |
+
**inputs,
|
23 |
+
max_new_tokens=200,
|
24 |
+
temperature=0.7,
|
25 |
+
do_sample=True,
|
26 |
+
top_p=0.9,
|
27 |
+
repetition_penalty=1.1,
|
28 |
+
eos_token_id=tokenizer.eos_token_id,
|
29 |
+
pad_token_id=tokenizer.eos_token_id
|
30 |
+
)
|
31 |
|
32 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
33 |
+
reply = output_text.split("Assistant:")[-1].strip()
|
34 |
+
history.append((message, reply))
|
35 |
+
return reply, history
|
36 |
|
37 |
+
# Gradio UI
|
38 |
+
iface = gr.ChatInterface(
|
39 |
+
fn=chat,
|
40 |
+
title="DeepSeek Chatbot",
|
41 |
+
description="Chatbot using DeepSeek 7B LLM",
|
42 |
+
theme="default"
|
43 |
+
)
|
44 |
|
|
|
45 |
if __name__ == "__main__":
|
46 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
-
torch
|
2 |
transformers
|
|
|
3 |
gradio
|
|
|
|
|
|
1 |
transformers
|
2 |
+
torch
|
3 |
gradio
|
4 |
+
accelerate
|