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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import urllib.request
|
| 3 |
+
import requests
|
| 4 |
+
import bs4
|
| 5 |
+
import lxml
|
| 6 |
+
import os
|
| 7 |
+
#import subprocess
|
| 8 |
+
from huggingface_hub import InferenceClient,HfApi
|
| 9 |
+
import random
|
| 10 |
+
import json
|
| 11 |
+
import datetime
|
| 12 |
+
#from query import tasks
|
| 13 |
+
from prompts import (
|
| 14 |
+
COMPRESS_DATA_PROMPT,
|
| 15 |
+
COMPRESS_DATA_PROMPT_SMALL,
|
| 16 |
+
LOG_PROMPT,
|
| 17 |
+
LOG_RESPONSE,
|
| 18 |
+
)
|
| 19 |
+
api=HfApi()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
client = InferenceClient(
|
| 24 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def parse_action(string: str):
|
| 28 |
+
print("PARSING:")
|
| 29 |
+
print(string)
|
| 30 |
+
assert string.startswith("action:")
|
| 31 |
+
idx = string.find("action_input=")
|
| 32 |
+
print(idx)
|
| 33 |
+
if idx == -1:
|
| 34 |
+
print ("idx == -1")
|
| 35 |
+
print (string[8:])
|
| 36 |
+
return string[8:], None
|
| 37 |
+
|
| 38 |
+
print ("last return:")
|
| 39 |
+
print (string[8 : idx - 1])
|
| 40 |
+
print (string[idx + 13 :].strip("'").strip('"'))
|
| 41 |
+
return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
VERBOSE = True
|
| 46 |
+
MAX_HISTORY = 100
|
| 47 |
+
MAX_DATA = 1000
|
| 48 |
+
|
| 49 |
+
def format_prompt(message, history):
|
| 50 |
+
prompt = "<s>"
|
| 51 |
+
for user_prompt, bot_response in history:
|
| 52 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 53 |
+
prompt += f" {bot_response}</s> "
|
| 54 |
+
prompt += f"[INST] {message} [/INST]"
|
| 55 |
+
return prompt
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def run_gpt(
|
| 59 |
+
prompt_template,
|
| 60 |
+
stop_tokens,
|
| 61 |
+
max_tokens,
|
| 62 |
+
seed,
|
| 63 |
+
purpose,
|
| 64 |
+
**prompt_kwargs,
|
| 65 |
+
):
|
| 66 |
+
print(seed)
|
| 67 |
+
generate_kwargs = dict(
|
| 68 |
+
temperature=0.9,
|
| 69 |
+
max_new_tokens=max_tokens,
|
| 70 |
+
top_p=0.95,
|
| 71 |
+
repetition_penalty=1.0,
|
| 72 |
+
do_sample=True,
|
| 73 |
+
seed=seed,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
content = PREFIX.format(
|
| 77 |
+
timestamp=timestamp,
|
| 78 |
+
purpose=purpose,
|
| 79 |
+
) + prompt_template.format(**prompt_kwargs)
|
| 80 |
+
if VERBOSE:
|
| 81 |
+
print(LOG_PROMPT.format(content))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 85 |
+
#formatted_prompt = format_prompt(f'{content}', history)
|
| 86 |
+
|
| 87 |
+
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 88 |
+
resp = ""
|
| 89 |
+
for response in stream:
|
| 90 |
+
resp += response.token.text
|
| 91 |
+
#yield resp
|
| 92 |
+
|
| 93 |
+
if VERBOSE:
|
| 94 |
+
print(LOG_RESPONSE.format(resp))
|
| 95 |
+
return resp
|
| 96 |
+
|
| 97 |
+
def compress_data(c,purpose, task, history):
|
| 98 |
+
seed=random.randint(1,1000000000)
|
| 99 |
+
|
| 100 |
+
print (c)
|
| 101 |
+
#tot=len(purpose)
|
| 102 |
+
#print(tot)
|
| 103 |
+
divr=int(c)/MAX_DATA
|
| 104 |
+
divi=int(divr)+1 if divr != int(divr) else int(divr)
|
| 105 |
+
chunk = int(int(c)/divr)
|
| 106 |
+
print(f'chunk:: {chunk}')
|
| 107 |
+
print(f'divr:: {divr}')
|
| 108 |
+
print (f'divi:: {divi}')
|
| 109 |
+
out = []
|
| 110 |
+
#out=""
|
| 111 |
+
s=0
|
| 112 |
+
e=chunk
|
| 113 |
+
print(f'e:: {e}')
|
| 114 |
+
new_history=""
|
| 115 |
+
task = f'Compile this data to fulfill the task: {task}, and complete the purpose: {purpose}\n'
|
| 116 |
+
for z in range(divi):
|
| 117 |
+
print(f's:e :: {s}:{e}')
|
| 118 |
+
|
| 119 |
+
hist = history[s:e]
|
| 120 |
+
|
| 121 |
+
resp = run_gpt(
|
| 122 |
+
COMPRESS_DATA_PROMPT_SMALL,
|
| 123 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
| 124 |
+
max_tokens=2048,
|
| 125 |
+
seed=seed,
|
| 126 |
+
purpose=purpose,
|
| 127 |
+
task=task,
|
| 128 |
+
knowledge=new_history,
|
| 129 |
+
history=hist,
|
| 130 |
+
)
|
| 131 |
+
new_history = resp
|
| 132 |
+
print (resp)
|
| 133 |
+
out+=resp
|
| 134 |
+
e=e+chunk
|
| 135 |
+
s=s+chunk
|
| 136 |
+
'''
|
| 137 |
+
resp = run_gpt(
|
| 138 |
+
COMPRESS_DATA_PROMPT,
|
| 139 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
| 140 |
+
max_tokens=1024,
|
| 141 |
+
seed=seed,
|
| 142 |
+
purpose=purpose,
|
| 143 |
+
task=task,
|
| 144 |
+
knowledge=new_history,
|
| 145 |
+
history="All data has been recieved.",
|
| 146 |
+
)'''
|
| 147 |
+
print ("final" + resp)
|
| 148 |
+
history = "observation: {}\n".format(resp)
|
| 149 |
+
return history
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def summarize(inp,file=None):
|
| 154 |
+
out = str(inp)
|
| 155 |
+
rl = len(out)
|
| 156 |
+
print(f'rl:: {rl}')
|
| 157 |
+
for i in str(out):
|
| 158 |
+
if i == " " or i=="," or i=="\n":
|
| 159 |
+
c +=1
|
| 160 |
+
print (f'c:: {c}')
|
| 161 |
+
if rl > MAX_DATA:
|
| 162 |
+
print("compressing...")
|
| 163 |
+
rawp = compress_data(c,purpose,task,out)
|
| 164 |
+
print (rawp)
|
| 165 |
+
print (f'out:: {out}')
|
| 166 |
+
#history += "observation: the search results are:\n {}\n".format(out)
|
| 167 |
+
task = "complete?"
|
| 168 |
+
return history
|
| 169 |
+
#################################
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
examples =[
|
| 173 |
+
"what are todays breaking news stories?",
|
| 174 |
+
"find the most popular model that I can use to generate an image by providing a text prompt",
|
| 175 |
+
"return the top 10 models that I can use to identify objects in images",
|
| 176 |
+
"which models have the most likes from each category?"
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
app = gr.ChatInterface(
|
| 181 |
+
fn=run,
|
| 182 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 183 |
+
title="Mixtral 46.7B Powered <br> Search",
|
| 184 |
+
examples=examples,
|
| 185 |
+
concurrency_limit=20,
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
'''
|
| 189 |
+
with gr.Blocks() as app:
|
| 190 |
+
with gr.Row():
|
| 191 |
+
inp_query=gr.Textbox()
|
| 192 |
+
models_dd=gr.Dropdown(choices=[m for m in return_list],interactive=True)
|
| 193 |
+
with gr.Row():
|
| 194 |
+
button=gr.Button()
|
| 195 |
+
stop_button=gr.Button("Stop")
|
| 196 |
+
text=gr.JSON()
|
| 197 |
+
inp_query.change(search_models,inp_query,models_dd)
|
| 198 |
+
go=button.click(test_fn,None,text)
|
| 199 |
+
stop_button.click(None,None,None,cancels=[go])
|
| 200 |
+
'''
|
| 201 |
+
app.launch(server_port=7860,show_api=False)
|