darpan-jain commited on
Commit
ae9cafc
1 Parent(s): 1e89e38

Add logging

Browse files
Files changed (1) hide show
  1. app_chat.py +19 -6
app_chat.py CHANGED
@@ -4,6 +4,13 @@ import torch
4
  import transformers
5
  import gradio as gr
6
  import time
 
 
 
 
 
 
 
7
 
8
  MODEL = "decapoda-research/llama-7b-hf"
9
  LORA_WEIGHTS = "tloen/alpaca-lora-7b"
@@ -11,11 +18,13 @@ device = "cpu"
11
  print(f"Model device = {device}", flush=True)
12
 
13
  def load_model():
 
14
  tokenizer = LlamaTokenizer.from_pretrained(MODEL)
15
  model = LlamaForCausalLM.from_pretrained(MODEL, device_map={"": device}, low_cpu_mem_usage=True)
16
  model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device}, torch_dtype=torch.float16)
17
  model.eval()
18
 
 
19
  return model, tokenizer
20
 
21
  def generate_prompt(input):
@@ -66,26 +75,30 @@ def eval_prompt(
66
  return bot_response
67
 
68
  def run_app(model, tokenizer):
 
 
 
69
  with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=True) as chat:
70
  chatbot = gr.Chatbot(label = "Alpaca Demo")
71
  msg = gr.Textbox(show_label = False, placeholder = "Enter your text here")
72
  clear = gr.Button("Clear")
73
- temparature = gr.Slider(minimum=0, maximum=1, value=0.8, label="Temparature")
74
 
75
  def user(user_msg, history):
 
76
  return "", history + [[user_msg, None]]
77
 
78
  def bot(history):
79
- print("Processing user input for Alpaca response...")
80
  last_input = history[-1][0]
81
- print(f"User input = {last_input}")
82
 
83
  tick = time.time()
84
  bot_response = eval_prompt(model, tokenizer, last_input)
85
- print(f"Inference time = {time.time() - tick} seconds")
86
 
87
  history[-1][1] = bot_response
88
- print("Response generated and added to history.\n")
 
89
  return history
90
 
91
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
@@ -94,12 +107,12 @@ def run_app(model, tokenizer):
94
 
95
  clear.click(lambda: None, None, chatbot, queue=False)
96
 
97
-
98
  chat.queue()
99
  chat.launch(share=True)
100
 
101
 
102
  if __name__ == "__main__":
 
103
  model, tokenizer = load_model()
104
 
105
  # Run the actual gradio app
 
4
  import transformers
5
  import gradio as gr
6
  import time
7
+ import logging
8
+
9
+ logging.basicConfig(level=logging.INFO)
10
+
11
+ # Dump logs to a file
12
+ logging.getLogger().addHandler(logging.FileHandler("app_chat.log"))
13
+
14
 
15
  MODEL = "decapoda-research/llama-7b-hf"
16
  LORA_WEIGHTS = "tloen/alpaca-lora-7b"
 
18
  print(f"Model device = {device}", flush=True)
19
 
20
  def load_model():
21
+ logging.info("Loading model...")
22
  tokenizer = LlamaTokenizer.from_pretrained(MODEL)
23
  model = LlamaForCausalLM.from_pretrained(MODEL, device_map={"": device}, low_cpu_mem_usage=True)
24
  model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device}, torch_dtype=torch.float16)
25
  model.eval()
26
 
27
+ logging.info("Model loaded.")
28
  return model, tokenizer
29
 
30
  def generate_prompt(input):
 
75
  return bot_response
76
 
77
  def run_app(model, tokenizer):
78
+
79
+ logging.info("Starting chat app...")
80
+
81
  with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=True) as chat:
82
  chatbot = gr.Chatbot(label = "Alpaca Demo")
83
  msg = gr.Textbox(show_label = False, placeholder = "Enter your text here")
84
  clear = gr.Button("Clear")
 
85
 
86
  def user(user_msg, history):
87
+ logging.info("User input received.")
88
  return "", history + [[user_msg, None]]
89
 
90
  def bot(history):
91
+ logging.info("Processing user input for Alpaca response...")
92
  last_input = history[-1][0]
93
+ logging.info(f"User input = {last_input}")
94
 
95
  tick = time.time()
96
  bot_response = eval_prompt(model, tokenizer, last_input)
97
+ logging.info(f"Inference time = {time.time() - tick} seconds")
98
 
99
  history[-1][1] = bot_response
100
+ logging.info("Response generated and added to history.\n")
101
+
102
  return history
103
 
104
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
 
107
 
108
  clear.click(lambda: None, None, chatbot, queue=False)
109
 
 
110
  chat.queue()
111
  chat.launch(share=True)
112
 
113
 
114
  if __name__ == "__main__":
115
+
116
  model, tokenizer = load_model()
117
 
118
  # Run the actual gradio app