sdgzero2ai commited on
Commit
7ec72c7
·
verified ·
1 Parent(s): a224e27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -21
app.py CHANGED
@@ -1,26 +1,50 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Create text-generation pipeline with explicit truncation.
5
- generator = pipeline(
6
- "text-generation",
7
- model="gpt2",
8
- max_length=100,
9
- truncation=True # important
10
- )
11
 
12
- def chatbot(prompt):
13
- # Generate text with the pipeline, also specifying truncation at call time (optional).
14
- responses = generator(prompt, max_length=100, truncation=True)
15
- return responses[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- demo = gr.Interface(
18
- fn=chatbot,
19
- inputs="text",
20
- outputs="text",
21
- title="Hugging Face GPT-2 Chatbot",
22
- description="Ask anything to the GPT-2-based model!"
23
- )
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- if __name__ == "__main__":
26
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ # Load the DeepSeek R1 model and tokenizer
6
+ model_name = "deepseek-ai/deepseek-r1"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
9
 
10
+ def chat_with_deepseek(user_input, history):
11
+ # Combine the history with the new user input
12
+ full_input = "\n".join(history + [user_input])
13
+
14
+ # Tokenize the input
15
+ inputs = tokenizer(full_input, return_tensors="pt")
16
+
17
+ # Generate a response
18
+ outputs = model.generate(inputs.input_ids, max_length=150, num_return_sequences=1)
19
+
20
+ # Decode the response
21
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+
23
+ # Update the history with the new interaction
24
+ history.append(user_input)
25
+ history.append(response)
26
+
27
+ return response, history
28
 
29
+ # Create the Gradio interface
30
+ with gr.Blocks() as demo:
31
+ chatbot = gr.Chatbot()
32
+ msg = gr.Textbox()
33
+ clear = gr.Button("Clear")
34
+
35
+ def user(user_message, history):
36
+ return "", history + [[user_message, None]]
37
+
38
+ def bot(history):
39
+ user_message = history[-1][0]
40
+ response, _ = chat_with_deepseek(user_message, [h[0] for h in history])
41
+ history[-1][1] = response
42
+ return history
43
+
44
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
45
+ bot, chatbot, chatbot
46
+ )
47
+ clear.click(lambda: None, None, chatbot, queue=False)
48
 
49
+ # Launch the Gradio app
50
+ demo.launch()