epicDev123 commited on
Commit
3f86740
·
verified ·
1 Parent(s): 08d326b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -46
app.py CHANGED
@@ -1,62 +1,43 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
3
  import torch
4
 
5
- model_id = "mistralai/Mistral-7B-Instruct"
6
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
9
-
10
- # Define modes
11
- modes = {
12
- "Standard Mode": {
13
- "prompt": "You are a helpful, honest, and friendly assistant.",
14
- "temperature": 0.7
15
- },
16
- "Brainstorming Mode": {
17
- "prompt": "You are a creative, out-of-the-box thinker helping users brainstorm ideas, explore thoughts, and be wildly imaginative.",
18
- "temperature": 1.0
19
- }
20
- }
21
-
22
- def chat(prompt, history=[], mode="Standard Mode"):
23
- settings = modes[mode]
24
- system_prompt = settings["prompt"]
25
- temp = settings["temperature"]
26
-
27
- full_prompt = system_prompt + "\n"
28
  for user, bot in history:
29
- full_prompt += f"User: {user}\nAssistant: {bot}\n"
30
- full_prompt += f"User: {prompt}\nAssistant:"
 
31
 
32
- inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
 
 
33
 
34
  output = model.generate(
35
  **inputs,
36
  max_new_tokens=300,
 
37
  do_sample=True,
38
- temperature=temp,
39
- top_p=0.95,
40
  pad_token_id=tokenizer.eos_token_id
41
  )
42
 
43
- decoded = tokenizer.decode(output[0], skip_special_tokens=True)
44
- response = decoded.split("Assistant:")[-1].strip()
45
- history.append((prompt, response))
46
- return response, history
47
-
48
- with gr.Blocks() as demo:
49
- mode_dropdown = gr.Dropdown(choices=list(modes.keys()), value="Standard Mode", label="Select Mode")
50
- chat_interface = gr.ChatInterface(fn=lambda message, history: chat(message, history, mode_dropdown.value))
51
-
52
- mode_dropdown.change(
53
- fn=lambda mode: None,
54
- inputs=mode_dropdown,
55
- outputs=[],
56
- queue=False
57
- )
58
-
59
- demo.add(mode_dropdown)
60
- demo.add(chat_interface)
61
 
62
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
+ model_id = "openchat/openchat-3.5-1210"
6
 
7
+ # Load model
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_id,
11
+ device_map="auto",
12
+ torch_dtype=torch.float16
13
+ )
14
+
15
+ # System prompt
16
+ def build_prompt(history, user_input):
17
+ system_prompt = "<|system|>\nYou are a helpful AI assistant.\n"
18
+ messages = system_prompt
19
+
 
 
 
 
 
 
 
 
 
20
  for user, bot in history:
21
+ messages += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"
22
+ messages += f"<|user|>\n{user_input}\n<|assistant|>\n"
23
+ return messages
24
 
25
+ def chat(user_input, history=[]):
26
+ prompt = build_prompt(history, user_input)
27
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
28
 
29
  output = model.generate(
30
  **inputs,
31
  max_new_tokens=300,
32
+ temperature=0.7,
33
  do_sample=True,
34
+ top_p=0.9,
 
35
  pad_token_id=tokenizer.eos_token_id
36
  )
37
 
38
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
39
+ answer = response.split("<|assistant|>")[-1].strip()
40
+ history.append((user_input, answer))
41
+ return answer, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ gr.ChatInterface(chat, title="OpenChat AI Assistant").launch()