Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -21,25 +21,27 @@ model = LlavaForConditionalGeneration.from_pretrained(model_id, low_cpu_mem_usag
|
|
21 |
model.to("cpu")
|
22 |
|
23 |
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
def extract_text_from_webpage(html_content):
|
45 |
soup = BeautifulSoup(html_content, 'html.parser')
|
@@ -93,20 +95,9 @@ def respond(message, history):
|
|
93 |
image = user_prompt["files"][0]
|
94 |
txt = user_prompt["text"]
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
if image.endswith(video_extensions):
|
101 |
-
gr.Info(f"Analyzing {video_extensions} file")
|
102 |
-
image = sample_frames(image)
|
103 |
-
image_tokens = "<image>" * int(len(image))
|
104 |
-
prompt = f"<|im_start|>user {image_tokens}\n{user_prompt}<|im_end|><|im_start|>assistant"
|
105 |
-
|
106 |
-
elif image.endswith(image_extensions):
|
107 |
-
gr.Info("Analyzing image")
|
108 |
-
image = Image.open(image).convert("RGB")
|
109 |
-
prompt = f"<|im_start|>user <image>\n{user_prompt}<|im_end|><|im_start|>assistant"
|
110 |
|
111 |
inputs = processor(prompt, image, return_tensors="pt")
|
112 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
|
@@ -119,64 +110,74 @@ def respond(message, history):
|
|
119 |
buffer = ""
|
120 |
for new_text in streamer:
|
121 |
buffer += new_text
|
122 |
-
yield buffer
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
messages
|
160 |
-
messages
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
messages = f"<|start_header_id|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You answers users query like human friend. You are also Expert in every field and also learn and try to answer from contexts related to previous question. Try your best to give best response possible to user. You also try to show emotions using Emojis and reply like human, use short forms, friendly tone and emotions.<|end_header_id|>"
|
181 |
for msg in history:
|
182 |
messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
|
@@ -188,18 +189,6 @@ def respond(message, history):
|
|
188 |
if not response.token.text == "<|eot_id|>":
|
189 |
output += response.token.text
|
190 |
yield output
|
191 |
-
except:
|
192 |
-
messages = f"<|start_header_id|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You answers users query like human friend. You are also Expert in every field and also learn and try to answer from contexts related to previous question. Try your best to give best response possible to user. You also try to show emotions using Emojis and reply like human, use short forms, friendly tone and emotions.<|end_header_id|>"
|
193 |
-
for msg in history:
|
194 |
-
messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
|
195 |
-
messages += f"\n<|start_header_id|>assistant\n{str(msg[1])}<|end_header_id|>"
|
196 |
-
messages+=f"\n<|start_header_id|>user\n{message_text} {vqa}<|end_header_id|>\n<|start_header_id|>assistant\n"
|
197 |
-
stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
|
198 |
-
output = ""
|
199 |
-
for response in stream:
|
200 |
-
if not response.token.text == "<|eot_id|>":
|
201 |
-
output += response.token.text
|
202 |
-
yield output
|
203 |
|
204 |
# Create the Gradio interface
|
205 |
demo = gr.ChatInterface(
|
@@ -214,7 +203,8 @@ demo = gr.ChatInterface(
|
|
214 |
{"text": "What's the current price of Bitcoin",},
|
215 |
{"text": "Create A Beautiful image of Effiel Tower at Night",},
|
216 |
{"text": "Write me a Python function to calculate the first 10 digits of the fibonacci sequence.",},
|
217 |
-
{"text": "What's the colour of
|
|
|
218 |
],
|
219 |
cache_examples=False,
|
220 |
)
|
|
|
21 |
model.to("cpu")
|
22 |
|
23 |
|
24 |
+
def llava(message):
|
25 |
+
if message["files"]:
|
26 |
+
image = user_prompt["files"][0]
|
27 |
+
txt = user_prompt["text"]
|
28 |
+
|
29 |
+
gr.Info("Analyzing image")
|
30 |
+
image = Image.open(image).convert("RGB")
|
31 |
+
prompt = f"<|im_start|>user <image>\n{user_prompt}<|im_end|><|im_start|>assistant"
|
32 |
+
|
33 |
+
inputs = processor(prompt, image, return_tensors="pt")
|
34 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
|
35 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
|
36 |
+
generated_text = ""
|
37 |
+
|
38 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
39 |
+
thread.start()
|
40 |
+
|
41 |
+
buffer = ""
|
42 |
+
for new_text in streamer:
|
43 |
+
buffer += new_text
|
44 |
+
yield buffer
|
45 |
|
46 |
def extract_text_from_webpage(html_content):
|
47 |
soup = BeautifulSoup(html_content, 'html.parser')
|
|
|
95 |
image = user_prompt["files"][0]
|
96 |
txt = user_prompt["text"]
|
97 |
|
98 |
+
gr.Info("Analyzing image")
|
99 |
+
image = Image.open(image).convert("RGB")
|
100 |
+
prompt = f"<|im_start|>user <image>\n{user_prompt}<|im_end|><|im_start|>assistant"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
inputs = processor(prompt, image, return_tensors="pt")
|
103 |
streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
|
|
|
110 |
buffer = ""
|
111 |
for new_text in streamer:
|
112 |
buffer += new_text
|
113 |
+
yield buffer
|
114 |
+
else:
|
115 |
+
functions_metadata = [
|
116 |
+
{"type": "function", "function": {"name": "web_search", "description": "Search query on google", "parameters": {"type": "object", "properties": {"query": {"type": "string", "description": "web search query"}}, "required": ["query"]}}},
|
117 |
+
{"type": "function", "function": {"name": "general_query", "description": "Reply general query of USER", "parameters": {"type": "object", "properties": {"prompt": {"type": "string", "description": "A detailed prompt"}}, "required": ["prompt"]}}},
|
118 |
+
{"type": "function", "function": {"name": "image_generation", "description": "Generate image for user", "parameters": {"type": "object", "properties": {"query": {"type": "string", "description": "image generation prompt"}, "number_of_image": {"type": "integer", "description": "number of images to generate"}}, "required": ["query"]}}},
|
119 |
+
{"type": "function", "function": {"name": "image_qna", "description": "Answer question asked by user related to image", "parameters": {"type": "object", "properties": {"query": {"type": "string", "description": "Question by user"}}, "required": ["query"]}}},
|
120 |
+
]
|
121 |
+
|
122 |
+
message_text = message["text"]
|
123 |
+
func_caller.append({"role": "user", "content": f'[SYSTEM]You are a helpful assistant. You have access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall> [USER] {message_text} {vqa}'})
|
124 |
+
|
125 |
+
response = client_gemma.chat_completion(func_caller, max_tokens=150)
|
126 |
+
response = str(response)
|
127 |
+
try:
|
128 |
+
response = response[int(response.find("{")):int(response.index("</"))]
|
129 |
+
except:
|
130 |
+
print("A error occured")
|
131 |
+
response = response.replace("\\n", "")
|
132 |
+
response = response.replace("\\'", "'")
|
133 |
+
response = response.replace('\\"', '"')
|
134 |
+
print(f"\n{response}")
|
135 |
+
|
136 |
+
func_caller.append({"role": "assistant", "content": f"<functioncall>{response}</functioncall>"})
|
137 |
+
|
138 |
+
try:
|
139 |
+
json_data = json.loads(str(response))
|
140 |
+
if json_data["name"] == "web_search":
|
141 |
+
query = json_data["arguments"]["query"]
|
142 |
+
gr.Info("Searching Web")
|
143 |
+
web_results = search(query)
|
144 |
+
gr.Info("Extracting relevant Info")
|
145 |
+
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
146 |
+
messages = f"<|im_start|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You are provided with WEB results from which you can find informations to answer users query in Structured and More better way. You do not say Unnecesarry things Only say thing which is important and relevant. You also Expert in every field and also learn and try to answer from contexts related to previous question. Try your best to give best response possible to user. You also try to show emotions using Emojis and reply like human, use short forms, friendly tone and emotions.<|im_end|>"
|
147 |
+
for msg in history:
|
148 |
+
messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
|
149 |
+
messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
|
150 |
+
messages+=f"\n<|im_start|>user\n{message_text} {vqa}<|im_end|>\n<|im_start|>web_result\n{web2}<|im_end|>\n<|im_start|>assistant\n"
|
151 |
+
stream = client_mixtral.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
|
152 |
+
output = ""
|
153 |
+
for response in stream:
|
154 |
+
if not response.token.text == "<|im_end|>":
|
155 |
+
output += response.token.text
|
156 |
+
yield output
|
157 |
+
elif json_data["name"] == "image_generation":
|
158 |
+
query = json_data["arguments"]["query"]
|
159 |
+
gr.Info("Generating Image, Please wait...")
|
160 |
+
seed = random.randint(1, 99999)
|
161 |
+
query = query.replace(" ", "%20")
|
162 |
+
image = f"![](https://image.pollinations.ai/prompt/{query}?seed={seed})"
|
163 |
+
yield image
|
164 |
+
time.sleep(5)
|
165 |
+
gr.Info("We are going to Update Our Image Generation Engine to more powerful ones in Next Update. ThankYou")
|
166 |
+
elif json_data["name"] == "image_qna":
|
167 |
+
messages = f"<|start_header_id|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You are provide with both images and captions and Your task is to answer of user with help of caption provided. Answer in human style and show emotions.<|end_header_id|>"
|
168 |
+
else:
|
169 |
+
messages = f"<|start_header_id|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You answers users query like human friend. You are also Expert in every field and also learn and try to answer from contexts related to previous question. Try your best to give best response possible to user. You also try to show emotions using Emojis and reply like human, use short forms, friendly tone and emotions.<|end_header_id|>"
|
170 |
+
for msg in history:
|
171 |
+
messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
|
172 |
+
messages += f"\n<|start_header_id|>assistant\n{str(msg[1])}<|end_header_id|>"
|
173 |
+
messages+=f"\n<|start_header_id|>user\n{message_text} {vqa}<|end_header_id|>\n<|start_header_id|>assistant\n"
|
174 |
+
stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
|
175 |
+
output = ""
|
176 |
+
for response in stream:
|
177 |
+
if not response.token.text == "<|eot_id|>":
|
178 |
+
output += response.token.text
|
179 |
+
yield output
|
180 |
+
except:
|
181 |
messages = f"<|start_header_id|>system\nYou are OpenGPT 4o mini a helpful assistant made by KingNish. You answers users query like human friend. You are also Expert in every field and also learn and try to answer from contexts related to previous question. Try your best to give best response possible to user. You also try to show emotions using Emojis and reply like human, use short forms, friendly tone and emotions.<|end_header_id|>"
|
182 |
for msg in history:
|
183 |
messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
|
|
|
189 |
if not response.token.text == "<|eot_id|>":
|
190 |
output += response.token.text
|
191 |
yield output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
# Create the Gradio interface
|
194 |
demo = gr.ChatInterface(
|
|
|
203 |
{"text": "What's the current price of Bitcoin",},
|
204 |
{"text": "Create A Beautiful image of Effiel Tower at Night",},
|
205 |
{"text": "Write me a Python function to calculate the first 10 digits of the fibonacci sequence.",},
|
206 |
+
{"text": "What's the colour of car in given image", "files": ["./car1.png"]},
|
207 |
+
{"text": "Read what's written on paper", "files": ["./paper_with_text.png"]},
|
208 |
],
|
209 |
cache_examples=False,
|
210 |
)
|