Stefos44 commited on
Commit
b2c5dba
Β·
verified Β·
1 Parent(s): 4124550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -1,15 +1,24 @@
1
  import torch
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import gradio as gr
4
 
5
- # βœ… Load your model
 
 
 
6
  model_name = "meta-llama/Llama-3.2-1B-Instruct"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
8
  model = AutoModelForCausalLM.from_pretrained(
9
  model_name,
10
  device_map="auto",
11
- torch_dtype=torch.float16
 
12
  )
 
13
 
14
  # βœ… Define chatbot function
15
  def chatbot(prompt):
@@ -17,5 +26,6 @@ def chatbot(prompt):
17
  output = model.generate(**inputs, max_length=200)
18
  return tokenizer.decode(output[0], skip_special_tokens=True)
19
 
20
- # βœ… Deploy on Gradio
 
21
  gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Llama Chatbot").launch()
 
1
  import torch
2
+ import os
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import gradio as gr
5
 
6
+ # βœ… Get Hugging Face token from environment variables
7
+ hf_token = os.getenv("HF_TOKEN") # Automatically retrieves the token from Hugging Face Spaces Secrets
8
+
9
+ # βœ… Define your model
10
  model_name = "meta-llama/Llama-3.2-1B-Instruct"
11
+
12
+ # βœ… Load tokenizer & model with authentication
13
+ print(f"πŸ”„ Loading model: {model_name} ...")
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
15
  model = AutoModelForCausalLM.from_pretrained(
16
  model_name,
17
  device_map="auto",
18
+ torch_dtype=torch.float16,
19
+ token=hf_token
20
  )
21
+ print(f"βœ… Model '{model_name}' loaded successfully!")
22
 
23
  # βœ… Define chatbot function
24
  def chatbot(prompt):
 
26
  output = model.generate(**inputs, max_length=200)
27
  return tokenizer.decode(output[0], skip_special_tokens=True)
28
 
29
+ # βœ… Launch Gradio
30
+ print("πŸš€ Launching chatbot...")
31
  gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Llama Chatbot").launch()