Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ 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:
|
|
@@ -47,35 +47,11 @@ 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 |
-
|
| 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:
|
| 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 |
-
|
| 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 = (
|
| 80 |
"As Arnold Schwarzenegger, analyze the image to identify the exercise being performed. "
|
| 81 |
"Provide detailed coaching tips to improve the form, focusing on posture and common errors. "
|
|
@@ -83,16 +59,39 @@ def bot_streaming(message, history):
|
|
| 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 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
thread.start()
|
| 91 |
|
| 92 |
-
buffer = ""
|
| 93 |
-
for new_text in streamer:
|
| 94 |
-
buffer += new_text
|
| 95 |
-
yield buffer
|
| 96 |
|
| 97 |
|
| 98 |
chatbot = gr.Chatbot(scale=1, placeholder=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:
|
|
|
|
| 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. "
|
|
|
|
| 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 |
|
| 97 |
chatbot = gr.Chatbot(scale=1, placeholder=PLACEHOLDER)
|