simonraj commited on
Commit
fc80d39
1 Parent(s): 5104567

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
app.py CHANGED
@@ -36,8 +36,6 @@ PLACEHOLDER = """
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,50 +45,45 @@ def bot_streaming(message, history):
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
- # Custom system prompt to guide the model's responses
55
- system_prompt = (
56
- "As Arnold Schwarzenegger, analyze the image to identify the exercise being performed. "
57
- "Provide detailed coaching tips to improve the form, focusing on posture and common errors. "
58
- "Use motivational and energetic language. If the image does not show an exercise, respond with: "
59
- "'What are you doing? This is no time for games! Upload a real exercise picture and let's pump it up!'"
60
- )
61
-
62
- # Create the conversation history for the prompt
63
  conversation = []
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  if len(history) == 0:
65
  conversation.append({"role": "user", "content": f"<|image_1|>\n{message['text']}"})
66
  else:
67
- for user, assistant in history:
68
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
69
- conversation.append({"role": "user", "content": f"<|image_1|>\n{message['text']}"})
70
-
71
- # Format the prompt as specified in the Phi model guidelines
72
- formatted_prompt = processor.tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
73
-
74
- # Open the image and prepare inputs
75
  image = Image.open(image)
76
- inputs = processor(formatted_prompt, images=image, return_tensors="pt").to("cuda:0")
77
-
78
- # Define generation arguments
79
- generation_args = {
80
- "max_new_tokens": 280,
81
- "temperature": 0.0,
82
- "do_sample": False,
83
- "eos_token_id": processor.tokenizer.eos_token_id,
84
- }
85
 
86
- # Generate the response
87
- generate_ids = model.generate(**inputs, **generation_args)
88
 
89
- # Process the generated IDs to get the response text
90
- generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
91
- response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
92
 
93
- yield response
 
 
 
94
 
95
 
96
 
 
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
  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 = []
55
+ flag = False
56
+ for user, assistant in history:
57
+ if assistant is None:
58
+ flag = True
59
+ conversation.extend([{"role": "user", "content": ""}])
60
+ continue
61
+ if flag == True:
62
+ conversation[0]['content'] = f"<|image_1|>\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_1|>\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
+ streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True, "skip_prompt": True, 'clean_up_tokenization_spaces': False,})
78
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=512, do_sample=False, temperature=0.0, eos_token_id=processor.tokenizer.eos_token_id,)
79
 
80
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
81
+ thread.start()
 
82
 
83
+ buffer = ""
84
+ for new_text in streamer:
85
+ buffer += new_text
86
+ yield buffer
87
 
88
 
89