Spaces:
Running
on
Zero
Running
on
Zero
Update chatbot.py
Browse files- chatbot.py +32 -6
chatbot.py
CHANGED
@@ -7,6 +7,7 @@ import random
|
|
7 |
from threading import Thread
|
8 |
from typing import List, Dict, Union
|
9 |
import subprocess
|
|
|
10 |
subprocess.run(
|
11 |
"pip install flash-attn --no-build-isolation",
|
12 |
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
@@ -278,6 +279,21 @@ def search(term, num_results=3, lang="en", advanced=True, timeout=5, safe="activ
|
|
278 |
all_results.append({"link": None, "text": None})
|
279 |
return all_results
|
280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
chat_history = []
|
282 |
history = ""
|
283 |
|
@@ -306,20 +322,25 @@ def model_inference(
|
|
306 |
|
307 |
# Web search logic
|
308 |
if not user_prompt["files"]:
|
309 |
-
if web_search:
|
310 |
"""Performs a web search, feeds the results to a language model, and returns the answer."""
|
311 |
web_results = search(user_prompt["text"])
|
312 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
313 |
# Load the language model
|
314 |
-
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.
|
315 |
generate_kwargs = dict(
|
316 |
max_new_tokens=4000,
|
317 |
do_sample=True,
|
318 |
)
|
319 |
# Format the prompt for the language model
|
320 |
-
formatted_prompt =
|
321 |
-
|
|
|
|
|
|
|
|
|
322 |
output = ""
|
|
|
323 |
for response in stream:
|
324 |
if not response.token.text == "</s>":
|
325 |
output += response.token.text
|
@@ -331,9 +352,14 @@ def model_inference(
|
|
331 |
do_sample=True,
|
332 |
)
|
333 |
# Format the prompt for the language model
|
334 |
-
formatted_prompt =
|
335 |
-
|
|
|
|
|
|
|
|
|
336 |
output = ""
|
|
|
337 |
for response in stream:
|
338 |
if not response.token.text == "</s>":
|
339 |
output += response.token.text
|
|
|
7 |
from threading import Thread
|
8 |
from typing import List, Dict, Union
|
9 |
import subprocess
|
10 |
+
# Install flash attention, skipping CUDA build if necessary
|
11 |
subprocess.run(
|
12 |
"pip install flash-attn --no-build-isolation",
|
13 |
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
|
|
279 |
all_results.append({"link": None, "text": None})
|
280 |
return all_results
|
281 |
|
282 |
+
# Format the prompt for the language model
|
283 |
+
def format_prompt(user_prompt, chat_history):
|
284 |
+
prompt = "<s>"
|
285 |
+
for item in chat_history:
|
286 |
+
# Check if the item is a tuple (text response)
|
287 |
+
if isinstance(item, tuple):
|
288 |
+
prompt += f"[INST] {item[0]} [/INST]" # User prompt
|
289 |
+
prompt += f" {item[1]}</s> " # Bot response
|
290 |
+
# Otherwise, assume it's related to an image - you might need to adjust this logic
|
291 |
+
else:
|
292 |
+
# Handle image representation in the prompt, e.g., add a placeholder
|
293 |
+
prompt += f" [Image] "
|
294 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
295 |
+
return prompt
|
296 |
+
|
297 |
chat_history = []
|
298 |
history = ""
|
299 |
|
|
|
322 |
|
323 |
# Web search logic
|
324 |
if not user_prompt["files"]:
|
325 |
+
if web_search is True:
|
326 |
"""Performs a web search, feeds the results to a language model, and returns the answer."""
|
327 |
web_results = search(user_prompt["text"])
|
328 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
329 |
# Load the language model
|
330 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
331 |
generate_kwargs = dict(
|
332 |
max_new_tokens=4000,
|
333 |
do_sample=True,
|
334 |
)
|
335 |
# Format the prompt for the language model
|
336 |
+
formatted_prompt = format_prompt(
|
337 |
+
f"""You are OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. You are provided with WEB info from which you can find informations to answer. You do not say Unnecesarry things Only say thing which is important and relevant. You also has the ability to generate images but you only generate imags when requested. BY Utilizing the following link structure, : ![](https://image.pollinations.ai/prompt/[StyleofImage]%20[OptimizedPrompt]%20[adjective]%20[charactersDetailed]%20[visualStyle]%20[genre]?width=[width]&height=[height]&nologo=poll&nofeed=yes&seed=[random]) For image generation, You replace info inside bracets with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience. Note: 1. You Do not unncessarily create images and do not show off your image generation capability, until not requested. 2. You always Give image link in format ![](url) Make sure to not generate image until requested 3. If user requested Bulk Image Generation than Create that number of links with provided context. 4. Always learn from previous conversation. 5. Always try to connect conversation with history. 6. Do not make conversation too long. 7. Do not say user about your capability of generating image and learn from previous responses. My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations.You remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question. Make sure to not generate image until requested [USER] {user_prompt} [WEB] {web2} [OpenGPT 4o]""",
|
338 |
+
chat_history)
|
339 |
+
# Generate the response from the language model
|
340 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
341 |
+
return_full_text=False)
|
342 |
output = ""
|
343 |
+
# Construct the output from the stream of tokens
|
344 |
for response in stream:
|
345 |
if not response.token.text == "</s>":
|
346 |
output += response.token.text
|
|
|
352 |
do_sample=True,
|
353 |
)
|
354 |
# Format the prompt for the language model
|
355 |
+
formatted_prompt = format_prompt(
|
356 |
+
f"""You are OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. You do not say Unnecesarry things Only say thing which is important and relevant. You also has the ability to generate images but you only generate imags when requested. BY Utilizing the following link structure, : ![](https://image.pollinations.ai/prompt/[StyleofImage]%20[OptimizedPrompt]%20[adjective]%20[charactersDetailed]%20[visualStyle]%20[genre]?width=[width]&height=[height]&nologo=poll&nofeed=yes&seed=[random]) For image generation, You replace info inside bracets with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience. Note: 1. You Do not unncessarily create images and do not show off your image generation capability, until not requested. 2. You always Give image link in format ![](url) 3. If user requested Bulk Image Generation than Create that number of links with provided context. 4. Always learn from previous conversation. 5. Always try to connect conversation with history. 6. Do not make conversation too long. 7. Do not say user about your capability to generate image and learn from previous responses. My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations. I am constantly evolving, ensuring that I remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question. {history} . [USER] {user_prompt} [OpenGPT 4o]""",
|
357 |
+
chat_history)
|
358 |
+
# Generate the response from the language model
|
359 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
360 |
+
return_full_text=False)
|
361 |
output = ""
|
362 |
+
# Construct the output from the stream of tokens
|
363 |
for response in stream:
|
364 |
if not response.token.text == "</s>":
|
365 |
output += response.token.text
|