Reality123b commited on
Commit
8afc2a3
·
verified ·
1 Parent(s): d53a7c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -5,10 +5,10 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
  # Set seed for reproducibility
6
  torch.random.manual_seed(0)
7
 
8
- # Load the model and tokenizer
9
  model = AutoModelForCausalLM.from_pretrained(
10
  "microsoft/Phi-3.5-mini-instruct",
11
- device_map="cpu",
12
  torch_dtype="auto",
13
  trust_remote_code=True,
14
  )
@@ -19,6 +19,7 @@ pipe = pipeline(
19
  "text-generation",
20
  model=model,
21
  tokenizer=tokenizer,
 
22
  )
23
 
24
  # System message (invisible to the user)
@@ -49,26 +50,30 @@ with gr.Blocks() as demo:
49
 
50
  with gr.Row():
51
  with gr.Column():
52
- chatbox = gr.Chatbot()
53
- input_box = gr.Textbox(label="Your Message")
54
  submit_btn = gr.Button("Submit")
55
 
56
  conversation_state = gr.State([]) # Maintain conversation history
57
 
 
58
  def update_conversation(user_input, history):
59
  if user_input.strip():
60
  history.append({"user_input": user_input})
61
  updated_history = chatbot_response(history)
62
- return updated_history, ""
63
- return history, ""
 
 
 
 
 
64
 
65
  submit_btn.click(
66
  update_conversation,
67
  inputs=[input_box, conversation_state],
68
- outputs=[conversation_state, input_box],
69
  )
70
-
71
- chatbox.update(chatbot_response(conversation_state))
72
 
73
  # Launch the interface
74
  demo.launch()
 
5
  # Set seed for reproducibility
6
  torch.random.manual_seed(0)
7
 
8
+ # Load the model and tokenizer (using CPU)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  "microsoft/Phi-3.5-mini-instruct",
11
+ device_map="cpu", # Use CPU
12
  torch_dtype="auto",
13
  trust_remote_code=True,
14
  )
 
19
  "text-generation",
20
  model=model,
21
  tokenizer=tokenizer,
22
+ device=-1 # CPU
23
  )
24
 
25
  # System message (invisible to the user)
 
50
 
51
  with gr.Row():
52
  with gr.Column():
53
+ chatbox = gr.Chatbot(label="Conversation")
54
+ input_box = gr.Textbox(label="Your Message", placeholder="Type your message here...")
55
  submit_btn = gr.Button("Submit")
56
 
57
  conversation_state = gr.State([]) # Maintain conversation history
58
 
59
+ # Function to update the conversation
60
  def update_conversation(user_input, history):
61
  if user_input.strip():
62
  history.append({"user_input": user_input})
63
  updated_history = chatbot_response(history)
64
+ # Format conversation history for the Chatbot component
65
+ formatted_conversation = [
66
+ (msg["user_input"], msg.get("assistant_reply", ""))
67
+ for msg in updated_history
68
+ ]
69
+ return formatted_conversation, updated_history, ""
70
+ return [], history, ""
71
 
72
  submit_btn.click(
73
  update_conversation,
74
  inputs=[input_box, conversation_state],
75
+ outputs=[chatbox, conversation_state, input_box],
76
  )
 
 
77
 
78
  # Launch the interface
79
  demo.launch()