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