simonraj commited on
Commit
b9ce51c
Β·
verified Β·
1 Parent(s): d4704ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -36,6 +36,8 @@ PLACEHOLDER = """
36
  def bot_streaming(message, history):
37
  print(f'message is - {message}')
38
  print(f'history is - {history}')
 
 
39
  if message["files"]:
40
  if type(message["files"][-1]) == dict:
41
  image = message["files"][-1]["path"]
@@ -45,10 +47,8 @@ def bot_streaming(message, history):
45
  for hist in history:
46
  if type(hist[0]) == tuple:
47
  image = hist[0][0]
48
- try:
49
- if image is None:
50
- raise gr.Error("You need to upload an image for Phi3-Vision to work. Close the error and try again with an Image.")
51
- except NameError:
52
  raise gr.Error("You need to upload an image for Phi3-Vision to work. Close the error and try again with an Image.")
53
 
54
  conversation = []
@@ -58,21 +58,22 @@ def bot_streaming(message, history):
58
  flag = True
59
  conversation.extend([{"role": "user", "content": ""}])
60
  continue
61
- if flag == True:
62
- conversation[0]['content'] = f"\n{user}"
63
  conversation.extend([{"role": "assistant", "content": assistant}])
64
  flag = False
65
  continue
66
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
67
 
68
  if len(history) == 0:
69
- conversation.append({"role": "user", "content": f"\n{message['text']}"})
70
  else:
71
  conversation.append({"role": "user", "content": message['text']})
 
72
  print(f"prompt is -\n{conversation}")
73
  prompt = processor.tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
74
  image = Image.open(image)
75
- inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
76
 
77
  # Custom prompt to ensure responses are in Arnold's style
78
  system_prompt = (
@@ -82,10 +83,10 @@ def bot_streaming(message, history):
82
  "'What are you doing? This is no time for games! Upload a real exercise picture and let's pump it up!'"
83
  )
84
 
85
- streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True, "skip_prompt": True, 'clean_up_tokenization_spaces': False,})
86
- generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=280, do_sample=False, temperature=0.0, eos_token_id=processor.tokenizer.eos_token_id,)
87
 
88
- thread = Thread(target=model.generate, kwargs=generation_kwargs, args=(system_prompt,))
89
  thread.start()
90
 
91
  buffer = ""
@@ -94,6 +95,7 @@ def bot_streaming(message, history):
94
  yield buffer
95
 
96
 
 
97
  chatbot = gr.Chatbot(scale=1, placeholder=PLACEHOLDER)
98
  chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
99
  with gr.Blocks(fill_height=True,) as demo:
 
36
  def bot_streaming(message, history):
37
  print(f'message is - {message}')
38
  print(f'history is - {history}')
39
+
40
+ image = None
41
  if message["files"]:
42
  if type(message["files"][-1]) == dict:
43
  image = message["files"][-1]["path"]
 
47
  for hist in history:
48
  if type(hist[0]) == tuple:
49
  image = hist[0][0]
50
+
51
+ if image is None:
 
 
52
  raise gr.Error("You need to upload an image for Phi3-Vision to work. Close the error and try again with an Image.")
53
 
54
  conversation = []
 
58
  flag = True
59
  conversation.extend([{"role": "user", "content": ""}])
60
  continue
61
+ if flag:
62
+ conversation[0]['content'] = f"<|image|>\n{user}"
63
  conversation.extend([{"role": "assistant", "content": assistant}])
64
  flag = False
65
  continue
66
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
67
 
68
  if len(history) == 0:
69
+ conversation.append({"role": "user", "content": f"<|image|>\n{message['text']}"})
70
  else:
71
  conversation.append({"role": "user", "content": message['text']})
72
+
73
  print(f"prompt is -\n{conversation}")
74
  prompt = processor.tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
75
  image = Image.open(image)
76
+ inputs = processor(prompt, images=image, return_tensors="pt").to("cuda:0")
77
 
78
  # Custom prompt to ensure responses are in Arnold's style
79
  system_prompt = (
 
83
  "'What are you doing? This is no time for games! Upload a real exercise picture and let's pump it up!'"
84
  )
85
 
86
+ streamer = TextIteratorStreamer(processor, skip_special_tokens=True, skip_prompt=True, clean_up_tokenization_spaces=False)
87
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=280, do_sample=False, temperature=0.0, eos_token_id=processor.tokenizer.eos_token_id)
88
 
89
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
90
  thread.start()
91
 
92
  buffer = ""
 
95
  yield buffer
96
 
97
 
98
+
99
  chatbot = gr.Chatbot(scale=1, placeholder=PLACEHOLDER)
100
  chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
101
  with gr.Blocks(fill_height=True,) as demo: