realitystar commited on
Commit
1db2e45
1 Parent(s): 09ac675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -20
app.py CHANGED
@@ -82,20 +82,104 @@ client_gemma = InferenceClient("google/gemma-1.1-7b-it")
82
  client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
83
  client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
84
 
85
- # Define the main chat function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  def respond(message, history):
87
  func_caller = []
88
 
89
  user_prompt = message
90
  # Handle image processing
91
- if message["files"]:
92
  inputs = llava(message, history)
93
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
94
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
95
 
96
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
97
  thread.start()
98
-
99
  buffer = ""
100
  for new_text in streamer:
101
  buffer += new_text
@@ -109,15 +193,14 @@ def respond(message, history):
109
  ]
110
 
111
  message_text = message["text"]
112
- func_caller.append({"role": "user", "content": f'[SYSTEM]You are a helpful AI assistant for a discord server called Stars Kingdom, your job is to have fun help users and listen to what they say or wan\'t you to act. You have been created by the discord server owner named Star. 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}'})
113
 
114
-
115
  response = client_gemma.chat_completion(func_caller, max_tokens=150)
116
  response = str(response)
117
  try:
118
  response = response[int(response.find("{")):int(response.index("</"))]
119
  except:
120
- print("A error occured")
121
  response = response.replace("\\n", "")
122
  response = response.replace("\\'", "'")
123
  response = response.replace('\\"', '"')
@@ -133,11 +216,11 @@ def respond(message, history):
133
  web_results = search(query)
134
  gr.Info("Extracting relevant Info")
135
  web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
136
- messages = f"<|im_start|>system\nYou are a helpful assistant made by Star. 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 fun and helping people 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 but only does this rarely don't overuse this, use short forms, friendly tone and emotions.<|im_end|>"
137
  for msg in history:
138
  messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
139
  messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
140
- messages+=f"\n<|im_start|>user\n{message_text}<|im_end|>\n<|im_start|>web_result\n{web2}<|im_end|>\n<|im_start|>assistant\n"
141
  stream = client_mixtral.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
142
  output = ""
143
  for response in stream:
@@ -161,17 +244,18 @@ def respond(message, history):
161
 
162
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
163
  thread.start()
164
-
165
  buffer = ""
166
  for new_text in streamer:
167
  buffer += new_text
168
  yield buffer
169
  else:
170
- messages = f"<|start_header_id|>system\nYou are a helpful assistant made by Star. 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 fun and helping people 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 but only does this rarely don't overuse this, use short forms, friendly tone and emotions.<|end_header_id|>"
 
171
  for msg in history:
172
- messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
173
- messages += f"\n<|start_header_id|>assistant\n{str(msg[1])}<|end_header_id|>"
174
- messages+=f"\n<|start_header_id|>user\n{message_text}<|end_header_id|>\n<|start_header_id|>assistant\n"
175
  stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
176
  output = ""
177
  for response in stream:
@@ -179,11 +263,11 @@ def respond(message, history):
179
  output += response.token.text
180
  yield output
181
  except:
182
- messages = f"<|start_header_id|>system\nYou are a helpful assistant made by Star. You answers users query like human friend. You also Expert in fun and helping people 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|>"
183
  for msg in history:
184
- messages += f"\n<|start_header_id|>user\n{str(msg[0])}<|end_header_id|>"
185
- messages += f"\n<|start_header_id|>assistant\n{str(msg[1])}<|end_header_id|>"
186
- messages+=f"\n<|start_header_id|>user\n{message_text}<|end_header_id|>\n<|start_header_id|>assistant\n"
187
  stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
188
  output = ""
189
  for response in stream:
@@ -191,11 +275,11 @@ def respond(message, history):
191
  output += response.token.text
192
  yield output
193
 
194
- # Create the Gradio interface
195
  demo = gr.ChatInterface(
196
  fn=respond,
197
  chatbot=gr.Chatbot(show_copy_button=True, likeable=True, layout="panel"),
198
- description ="# Star's Kingdom ai bot",
199
  textbox=gr.MultimodalTextbox(),
200
  multimodal=True,
201
  concurrency_limit=200,
@@ -204,8 +288,9 @@ demo = gr.ChatInterface(
204
  {"text": "What's the preferred shirt color for an interview?",},
205
  {"text": "How can I dress more smartly?",},
206
  {"text": "Tell about some good accessories for a traditional Indian wedding",},
207
- {"text": "What's the colour of the frock in the given image?", "files": ["./frock.png"]},
208
  ],
209
  cache_examples=False,
210
  )
 
211
  demo.launch(share=True)
 
82
  client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
83
  client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
84
 
85
+ import gradio as gr
86
+ from huggingface_hub import InferenceClient
87
+ import json
88
+ import uuid
89
+ from PIL import Image
90
+ from bs4 import BeautifulSoup
91
+ import requests
92
+ import random
93
+ from transformers import LlavaProcessor, LlavaForConditionalGeneration, TextIteratorStreamer
94
+ from threading import Thread
95
+ import re
96
+ import time
97
+ import torch
98
+ import cv2
99
+
100
+ model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
101
+
102
+ processor = LlavaProcessor.from_pretrained(model_id)
103
+
104
+ model = LlavaForConditionalGeneration.from_pretrained(model_id)
105
+ model.to("cpu")
106
+
107
+
108
+ def llava(message, history):
109
+ if message["files"]:
110
+ image = message["files"][0]
111
+ else:
112
+ for hist in history:
113
+ if isinstance(hist[0], tuple):
114
+ image = hist[0][0]
115
+
116
+ txt = message["text"]
117
+
118
+ gr.Info("Analyzing image")
119
+ image = Image.open(image).convert("RGB")
120
+ prompt = f"<|im_start|>user <image>\n{txt}<|im_end|><|im_start|>assistant"
121
+
122
+ inputs = processor(prompt, image, return_tensors="pt")
123
+
124
+ # Return the dictionary format expected by MultimodalTextbox
125
+ return {"text": txt, "files": [image]}
126
+
127
+
128
+ def extract_text_from_webpage(html_content):
129
+ soup = BeautifulSoup(html_content, 'html.parser')
130
+ for tag in soup(["script", "style", "header", "footer"]):
131
+ tag.extract()
132
+ return soup.get_text(strip=True)
133
+
134
+ def search(query):
135
+ term = query
136
+ start = 0
137
+ all_results = []
138
+ max_chars_per_page = 8000
139
+ with requests.Session() as session:
140
+ resp = session.get(
141
+ url="https://www.google.com/search",
142
+ headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"},
143
+ params={"q": term, "num": 3, "udm": 14},
144
+ timeout=5,
145
+ verify=None,
146
+ )
147
+ resp.raise_for_status()
148
+ soup = BeautifulSoup(resp.text, "html.parser")
149
+ result_block = soup.find_all("div", attrs={"class": "g"})
150
+ for result in result_block:
151
+ link = result.find("a", href=True)
152
+ link = link["href"]
153
+ try:
154
+ webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"}, timeout=5, verify=False)
155
+ webpage.raise_for_status()
156
+ visible_text = extract_text_from_webpage(webpage.text)
157
+ if len(visible_text) > max_chars_per_page:
158
+ visible_text = visible_text[:max_chars_per_page]
159
+ all_results.append({"link": link, "text": visible_text})
160
+ except requests.exceptions.RequestException:
161
+ all_results.append({"link": link, "text": None})
162
+ return all_results
163
+
164
+ # Initialize inference clients for different models
165
+ client_gemma = InferenceClient("google/gemma-1.1-7b-it")
166
+ client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
167
+ client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
168
+
169
+ # Corrected the response format to ensure it works with Gradio's multimodal interface.
170
  def respond(message, history):
171
  func_caller = []
172
 
173
  user_prompt = message
174
  # Handle image processing
175
+ if message.get("files"):
176
  inputs = llava(message, history)
177
  streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
178
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
179
 
180
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
181
  thread.start()
182
+
183
  buffer = ""
184
  for new_text in streamer:
185
  buffer += new_text
 
193
  ]
194
 
195
  message_text = message["text"]
196
+ func_caller.append({"role": "user", "content": f'[SYSTEM]You are a helpful AI assistant for a discord server called Stars Kingdom, your job is to have fun help users and listen to what they say or want you to act. You have been created by the discord server owner named Star. 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_2": "value_2", ... }} }} </functioncall> [USER] {message_text}'})
197
 
 
198
  response = client_gemma.chat_completion(func_caller, max_tokens=150)
199
  response = str(response)
200
  try:
201
  response = response[int(response.find("{")):int(response.index("</"))]
202
  except:
203
+ print("An error occurred")
204
  response = response.replace("\\n", "")
205
  response = response.replace("\\'", "'")
206
  response = response.replace('\\"', '"')
 
216
  web_results = search(query)
217
  gr.Info("Extracting relevant Info")
218
  web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
219
+ messages = f"<|im_start|>system\nYou are a helpful assistant made by Star. You are provided with WEB results from which you can find informations to answer users query in a structured and better way. Only respond with what’s important!<|im_end|>"
220
  for msg in history:
221
  messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
222
  messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
223
+ messages += f"\n<|im_start|>user\n{message_text}<|im_end|>\n<|im_start|>web_result\n{web2}<|im_end|>\n<|im_start|>assistant\n"
224
  stream = client_mixtral.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
225
  output = ""
226
  for response in stream:
 
244
 
245
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
246
  thread.start()
247
+
248
  buffer = ""
249
  for new_text in streamer:
250
  buffer += new_text
251
  yield buffer
252
  else:
253
+ # Default response from llama model if no specific function matched
254
+ messages = f"<|im_start|>system\nYou are a helpful assistant made by Star.<|im_end|>"
255
  for msg in history:
256
+ messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
257
+ messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
258
+ messages += f"\n<|im_start|>user\n{message_text}<|im_end|>\n<|im_start|>assistant\n"
259
  stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
260
  output = ""
261
  for response in stream:
 
263
  output += response.token.text
264
  yield output
265
  except:
266
+ messages = f"<|im_start|>system\nYou are a helpful assistant made by Star. You answer users' queries like a human friend.<|im_end|>"
267
  for msg in history:
268
+ messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
269
+ messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
270
+ messages += f"\n<|im_start|>user\n{message_text}<|im_end|>\n<|im_start|>assistant\n"
271
  stream = client_llama.text_generation(messages, max_new_tokens=2000, do_sample=True, stream=True, details=True, return_full_text=False)
272
  output = ""
273
  for response in stream:
 
275
  output += response.token.text
276
  yield output
277
 
278
+ # Gradio Chat Interface
279
  demo = gr.ChatInterface(
280
  fn=respond,
281
  chatbot=gr.Chatbot(show_copy_button=True, likeable=True, layout="panel"),
282
+ description="AI assistant for Stars Kingdom",
283
  textbox=gr.MultimodalTextbox(),
284
  multimodal=True,
285
  concurrency_limit=200,
 
288
  {"text": "What's the preferred shirt color for an interview?",},
289
  {"text": "How can I dress more smartly?",},
290
  {"text": "Tell about some good accessories for a traditional Indian wedding",},
291
+ {"text": "What's the color of the frock in the given image?", "files": ["./frock.png"]},
292
  ],
293
  cache_examples=False,
294
  )
295
+
296
  demo.launch(share=True)