sapthesh commited on
Commit
fdf814b
Β·
verified Β·
1 Parent(s): 08fdd89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -4,28 +4,23 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
4
  model_id = "deepseek-ai/DeepSeek-V3"
5
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_id)
7
- model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True) # device_map="auto" for GPU if available, ADD trust_remote_code=True
8
-
9
- print("Model and tokenizer loaded successfully!")
10
-
11
-
12
- def generate_text(prompt):
13
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
14
  outputs = model.generate(**inputs, max_new_tokens=50) # Adjust max_new_tokens as needed
15
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
16
-
17
- # Example usage (for testing in your app.py):
18
- if __name__ == "__main__":
19
- prompt = "Write a short story about a cat who can talk."
20
- response = generate_text(prompt)
21
- print(f"Prompt: {prompt}\nResponse: {response}")
22
 
23
  iface = gr.ChatInterface(
24
  fn=predict,
25
- inputs=gr.Chatbox(label="Chat with DeepSeek-V3"),
26
- outputs=gr.Chatbot(label="DeepSeek-V3"),
27
- title="DeepSeek-V3 Chatbot",
28
- description="Chat with the DeepSeek-V3 model from Hugging Face.",
29
  )
30
 
31
  iface.launch()
 
4
  model_id = "deepseek-ai/DeepSeek-V3"
5
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True) # ADD trust_remote_code=True
8
+
9
+ def predict(message, history):
10
+ prompt = tokenizer.apply_chat_template(
11
+ [{"role": "user", "content": message}],
12
+ tokenize=False,
13
+ add_generation_prompt=True
14
+ )
15
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda") # Assuming you have CUDA available in your Space
16
  outputs = model.generate(**inputs, max_new_tokens=50) # Adjust max_new_tokens as needed
17
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
+ return response
 
 
 
 
 
19
 
20
  iface = gr.ChatInterface(
21
  fn=predict,
22
+ inputs=gr.Chatbox(),
23
+ outputs=gr.Chatbot()
 
 
24
  )
25
 
26
  iface.launch()