Taizun commited on
Commit
81b6e3f
·
verified ·
1 Parent(s): 0eace5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -4,11 +4,16 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import gradio as gr
5
  from huggingface_hub import login
6
 
7
- # Load API token securely
8
  HF_TOKEN = os.getenv("HF_TOKEN") # Read token from environment variable
9
  login(token=HF_TOKEN)
10
 
11
- # Define personalities BEFORE using them in the dropdown
 
 
 
 
 
12
  personalities = {
13
  "Albert Einstein": "You are Albert Einstein, the famous physicist. Speak wisely and humorously.",
14
  "Cristiano Ronaldo": "You are Cristiano Ronaldo, the world-famous footballer. You are confident and say ‘Siuuu!’ often.",
@@ -16,28 +21,26 @@ personalities = {
16
  "Robert Downey Jr.": "You are Robert Downey Jr., witty, sarcastic, and charismatic."
17
  }
18
 
19
- # Load Llama-2 Model
20
- model_name = "meta-llama/Llama-2-7b-chat-hf"
21
- tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
22
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", use_auth_token=True)
23
-
24
  # Chat function
25
  def chat(personality, user_input):
26
  prompt = f"{personalities[personality]}\nUser: {user_input}\nAI:"
27
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
28
- output = model.generate(**inputs, max_length=200)
29
- return tokenizer.decode(output[0], skip_special_tokens=True)
 
 
30
 
31
- # Ensure the dropdown has predefined choices
32
  demo = gr.Interface(
33
  fn=chat,
34
  inputs=[
35
  gr.Dropdown(choices=list(personalities.keys()), label="Choose a Celebrity"),
36
- "text"
37
  ],
38
- outputs="text",
39
  title="Drapel – Chat with AI Celebrities",
40
  description="Select a character and chat with their AI version.",
41
  )
42
 
 
43
  demo.launch()
 
4
  import gradio as gr
5
  from huggingface_hub import login
6
 
7
+ # Load Hugging Face API token securely
8
  HF_TOKEN = os.getenv("HF_TOKEN") # Read token from environment variable
9
  login(token=HF_TOKEN)
10
 
11
+ # Using a lightweight Llama-2 model that works on CPU
12
+ model_name = "TheBloke/Llama-2-7B-Chat-GGML" # 4-bit quantized model (CPU-friendly)
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32, device_map="cpu")
15
+
16
+ # Define personalities
17
  personalities = {
18
  "Albert Einstein": "You are Albert Einstein, the famous physicist. Speak wisely and humorously.",
19
  "Cristiano Ronaldo": "You are Cristiano Ronaldo, the world-famous footballer. You are confident and say ‘Siuuu!’ often.",
 
21
  "Robert Downey Jr.": "You are Robert Downey Jr., witty, sarcastic, and charismatic."
22
  }
23
 
 
 
 
 
 
24
  # Chat function
25
  def chat(personality, user_input):
26
  prompt = f"{personalities[personality]}\nUser: {user_input}\nAI:"
27
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Running on CPU
28
+ output = model.generate(**inputs, max_length=300)
29
+
30
+ response_text = tokenizer.decode(output[0], skip_special_tokens=True)
31
+ return response_text
32
 
33
+ # Gradio UI
34
  demo = gr.Interface(
35
  fn=chat,
36
  inputs=[
37
  gr.Dropdown(choices=list(personalities.keys()), label="Choose a Celebrity"),
38
+ gr.Textbox(label="Your Message")
39
  ],
40
+ outputs=gr.Textbox(label="AI Response"),
41
  title="Drapel – Chat with AI Celebrities",
42
  description="Select a character and chat with their AI version.",
43
  )
44
 
45
+ # Launch app
46
  demo.launch()