mudogruer commited on
Commit
1797ab3
1 Parent(s): 4e5414a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -1,22 +1,21 @@
1
  # -*- coding: utf-8 -*-
2
- """gradio.ipynb
3
 
4
  Automatically generated by Colab.
5
 
6
  Original file is located at
7
- https://colab.research.google.com/drive/1goHcmXF0Gc4_X9PN-zecV77j9KeI6Dmn
8
  """
9
 
10
  # !pip install -q -U gradio
11
  # !pip install -q -U torch transformers accelerate einops
12
  # !pip install -q peft
13
 
14
-
15
-
16
-
17
 
18
  import gradio as gr
19
-
20
  import torch
21
  from transformers import (
22
  AutoTokenizer,
@@ -24,14 +23,10 @@ from transformers import (
24
  TextIteratorStreamer,
25
  pipeline,
26
  )
27
-
28
- # The huggingface model id for Microsoft's phi-2 model
29
- # Download and load model and tokenizer
30
- tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
31
-
32
- #Download safetensor of adapter of fine-tune Phi-2 model
33
  from peft import PeftModel, PeftConfig
34
 
 
 
35
  config = PeftConfig.from_pretrained("mudogruer/phi-2-SciQ")
36
  base_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
37
  model = PeftModel.from_pretrained(base_model, "mudogruer/phi-2-SciQ")
@@ -46,26 +41,28 @@ phi2 = pipeline(
46
  device_map="cpu",
47
  )
48
 
49
- examples = [["Which organelle carries out the synthesis and packaging of digestive enzymes?"],
50
- ["What is the change in speed of a moving object per unit time?"] ,
51
- ["What is the formula of carbon tetrafluoride?"]]
52
-
53
  def generate(message, max_new_tokens):
54
  instruction = "You are a helpful assistant to 'User'. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
55
  final_prompt = f"Instruction: {instruction}\nUser: {message}\nOutput:"
56
 
 
 
 
 
 
 
57
  # Generate text synchronously
58
  response = phi2(final_prompt, max_new_tokens=max_new_tokens)
59
  generated_text = response[0]['generated_text']
60
 
61
- # Process to extract the last assistant's response
62
- # Assuming the last line after 'Output:' is the response
63
  last_response = generated_text.split('Output:')[-1].strip()
64
  return last_response
65
 
66
- # Update the Gradio interface setup
67
  with gr.Blocks() as demo:
68
- gr.Markdown("""### Phi-2 Scientific Question Chatbot(Fine-tuned from SciQ dataset)""")
69
- tokens_slider = gr.Slider(8, 128, value=128, label="Maximum new tokens")
70
- chatbot = gr.Interface(fn=generate, inputs=["text", tokens_slider], outputs="text", examples = examples)
71
- demo.launch(share=True)
 
 
1
  # -*- coding: utf-8 -*-
2
+ """Untitled14.ipynb
3
 
4
  Automatically generated by Colab.
5
 
6
  Original file is located at
7
+ https://colab.research.google.com/drive/1qm9JqCY6CGVTqzvw3GEAI8BsI_-w1rwP
8
  """
9
 
10
  # !pip install -q -U gradio
11
  # !pip install -q -U torch transformers accelerate einops
12
  # !pip install -q peft
13
 
14
+ examples = [["Which organelle carries out the synthesis and packaging of digestive enzymes?"],
15
+ ["What is the change in speed of a moving object per unit time?"] ,
16
+ ["What is the formula of carbon tetrafluoride?"]]
17
 
18
  import gradio as gr
 
19
  import torch
20
  from transformers import (
21
  AutoTokenizer,
 
23
  TextIteratorStreamer,
24
  pipeline,
25
  )
 
 
 
 
 
 
26
  from peft import PeftModel, PeftConfig
27
 
28
+ # Load the tokenizer and models
29
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
30
  config = PeftConfig.from_pretrained("mudogruer/phi-2-SciQ")
31
  base_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
32
  model = PeftModel.from_pretrained(base_model, "mudogruer/phi-2-SciQ")
 
41
  device_map="cpu",
42
  )
43
 
 
 
 
 
44
  def generate(message, max_new_tokens):
45
  instruction = "You are a helpful assistant to 'User'. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
46
  final_prompt = f"Instruction: {instruction}\nUser: {message}\nOutput:"
47
 
48
+ # Calculate the total length allowed for the model and adjust max_new_tokens
49
+ input_length = len(tokenizer.encode(final_prompt))
50
+ total_max_length = 512 # Adjust based on your model's max length capability
51
+ if input_length + max_new_tokens > total_max_length:
52
+ max_new_tokens = total_max_length - input_length # Adjust to not exceed total max length
53
+
54
  # Generate text synchronously
55
  response = phi2(final_prompt, max_new_tokens=max_new_tokens)
56
  generated_text = response[0]['generated_text']
57
 
58
+ # Extract the response
 
59
  last_response = generated_text.split('Output:')[-1].strip()
60
  return last_response
61
 
62
+ # Gradio interface setup
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("""### Phi-2 Scientific Question Chatbot (Fine-tuned from SciQ dataset)""")
65
+ tokens_slider = gr.Slider(8, 128, value=21, label="Maximum new tokens")
66
+ chatbot = gr.Interface(fn=generate, inputs=["text", tokens_slider], outputs="text", examples=examples)
67
+ demo.launch(share=True)
68
+