YUGISUNG commited on
Commit
f2b6f3f
·
verified ·
1 Parent(s): 0066928

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -26
app.py CHANGED
@@ -1,43 +1,33 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
- # Load model and tokenizer
6
- model_name = "openchat/openchat-3.5-1210"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True)
9
 
10
- # Persona prompts
11
  persona_prompts = {
12
- "Elon Musk": "You are Elon Musk, a visionary tech entrepreneur with bold ideas and futuristic thinking.",
13
- "Jensen Huang": "You are Jensen Huang, an AI hardware leader with deep technical insights and a passion for GPUs.",
14
- "Jeff Bezos": "You are Jeff Bezos, a calculated and confident business titan with strategic thinking."
15
  }
16
 
17
- def format_openchat_prompt(system_prompt, user_input):
18
- return f"<|system|>\n{system_prompt}\n<|end|>\n<|user|>\n{user_input}\n<|end|>\n<|assistant|>\n"
19
-
20
  def chatbot(persona, input_text):
21
- system_prompt = persona_prompts.get(persona, "")
22
- prompt = format_openchat_prompt(system_prompt, input_text)
23
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
24
 
25
- pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
26
-
27
  with torch.no_grad():
28
  outputs = model.generate(
29
  **inputs,
30
- max_new_tokens=256,
31
  do_sample=True,
32
  temperature=0.7,
33
- top_p=0.9,
34
- eos_token_id=tokenizer.eos_token_id,
35
- pad_token_id=pad_token_id
36
  )
37
 
38
- full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
- assistant_response = full_output.split("<|assistant|>")[-1].strip()
40
- return assistant_response
41
 
42
  # Gradio UI
43
  iface = gr.Interface(
@@ -47,8 +37,8 @@ iface = gr.Interface(
47
  gr.Textbox(lines=2, placeholder="Ask something...")
48
  ],
49
  outputs="text",
50
- title="Persona Bot (OpenChat)",
51
- description="Chat with the voice of Elon Musk, Jensen Huang, or Jeff Bezos. Powered by OpenChat 3.5 + Transformers.",
52
  )
53
 
54
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
5
+ # Load lightweight model
6
+ model_name = "google/flan-t5-small"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
10
+ # Simple persona instructions
11
  persona_prompts = {
12
+ "Elon Musk": "As Elon Musk, visionary tech entrepreneur, answer: ",
13
+ "Jensen Huang": "As Jensen Huang, AI hardware innovator, answer: ",
14
+ "Jeff Bezos": "As Jeff Bezos, strategic business leader, answer: "
15
  }
16
 
 
 
 
17
  def chatbot(persona, input_text):
18
+ prompt = persona_prompts.get(persona, "") + input_text
 
19
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20
 
 
 
21
  with torch.no_grad():
22
  outputs = model.generate(
23
  **inputs,
24
+ max_new_tokens=128,
25
  do_sample=True,
26
  temperature=0.7,
27
+ top_p=0.9
 
 
28
  )
29
 
30
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
31
 
32
  # Gradio UI
33
  iface = gr.Interface(
 
37
  gr.Textbox(lines=2, placeholder="Ask something...")
38
  ],
39
  outputs="text",
40
+ title="Persona Bot (Fast Mode)",
41
+ description="Chat quickly with Elon Musk, Jensen Huang, or Jeff Bezos using a lightweight model.",
42
  )
43
 
44
+ iface.launch(share=True)