Spaces:
Running
Running
Upload 7 files
Browse files- GPTSimple.py +101 -0
- app.py +75 -0
- delete.svg +1 -0
- requirements.txt +3 -0
- retry.svg +1 -0
- style.css +62 -0
- vision.py +25 -0
GPTSimple.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
base_urls = {'deepinfra':"https://api.deepinfra.com/v1/openai/chat/completions", "openai":"https://api.openai.com/v1/chat/completions"}
|
4 |
+
def print_token(token):
|
5 |
+
if token.token == None:
|
6 |
+
print()
|
7 |
+
else:
|
8 |
+
print(token.token, end="", flush=True)
|
9 |
+
def get_direct_output(history, model, api_key, stream = False, base_url="openai"):
|
10 |
+
if base_url in base_urls:
|
11 |
+
url = base_urls[base_url]
|
12 |
+
else:
|
13 |
+
url = base_url
|
14 |
+
headers = {
|
15 |
+
"Content-Type": "application/json",
|
16 |
+
"Authorization": f"Bearer {api_key}"
|
17 |
+
}
|
18 |
+
|
19 |
+
data = {
|
20 |
+
"model": model,
|
21 |
+
"stream":stream,
|
22 |
+
"messages": history
|
23 |
+
}
|
24 |
+
|
25 |
+
response = requests.post(url, json=data, headers=headers, stream=stream)
|
26 |
+
if stream:
|
27 |
+
return response
|
28 |
+
return response.json()
|
29 |
+
class conversation:
|
30 |
+
class token:
|
31 |
+
def __init__(self, line):
|
32 |
+
if line['choices'][0]['finish_reason'] == "stop":
|
33 |
+
self.token = None
|
34 |
+
self.model = line["model"]
|
35 |
+
self.message = {'role':'assistant','content':None}
|
36 |
+
self.response = line
|
37 |
+
else:
|
38 |
+
self.token = line["choices"][0]['delta']['content']
|
39 |
+
self.model = line["model"]
|
40 |
+
self.message = line["choices"][0]['delta']
|
41 |
+
self.response = line
|
42 |
+
def streamingResponse(self, lines, invis):
|
43 |
+
message = ""
|
44 |
+
iters = lines.iter_lines(decode_unicode=True)
|
45 |
+
for line in iters:
|
46 |
+
if 'data: ' not in line:
|
47 |
+
continue
|
48 |
+
line_js = json.loads(line.split('data: ')[1])
|
49 |
+
if line_js['choices'][0]['finish_reason'] == "stop":
|
50 |
+
if not invis:
|
51 |
+
self.history.append({'role':'assistant', 'content':message})
|
52 |
+
yield self.token(line_js)
|
53 |
+
break
|
54 |
+
token = self.token(line_js)
|
55 |
+
message += token.token
|
56 |
+
yield token
|
57 |
+
class response:
|
58 |
+
def __init__(self, json):
|
59 |
+
self.response = json
|
60 |
+
self.model = json['model']
|
61 |
+
self.id = json['id']
|
62 |
+
self.choices = json['choices']
|
63 |
+
self.text = json['choices'][0]['message']['content']
|
64 |
+
self.message = json['choices'][0]['message']
|
65 |
+
self.usage = json['usage']
|
66 |
+
self.prompt_tokens = json['usage']['prompt_tokens']
|
67 |
+
self.output_tokens = json['usage']['completion_tokens']
|
68 |
+
self.total_tokens = json['usage']['total_tokens']
|
69 |
+
def __init__(self, api_key='', model='gpt-3.5-turbo', history=None, system_prompt="You are a helpful assistant", base_url="openai"):
|
70 |
+
if base_url.lower() == "deepinfra" and model == "gpt-3.5-turbo":
|
71 |
+
model = "meta-llama/Llama-2-70b-chat-hf"
|
72 |
+
self.base_url = base_url.lower()
|
73 |
+
self.api_key = api_key
|
74 |
+
self.model = model
|
75 |
+
self.history = [{'role':'system',"content":system_prompt}]
|
76 |
+
if history is not None:
|
77 |
+
self.history = history
|
78 |
+
def generate(self, invisible=False, stream=False):
|
79 |
+
if stream:
|
80 |
+
res = self.streamingResponse(get_direct_output(self.history, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
|
81 |
+
else:
|
82 |
+
res = self.response(get_direct_output(self.history, self.model, self.api_key, base_url=self.base_url))
|
83 |
+
if not invisible:
|
84 |
+
self.history.append(res.message)
|
85 |
+
return res
|
86 |
+
def ask(self, message, invisible=False, stream=False):
|
87 |
+
|
88 |
+
if invisible:
|
89 |
+
out = self.history.copy()
|
90 |
+
out.append({"role":"user", "content":message})
|
91 |
+
else:
|
92 |
+
self.history.append({"role":"user", "content":message})
|
93 |
+
out = self.history
|
94 |
+
if stream:
|
95 |
+
res = self.streamingResponse(get_direct_output(out, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
|
96 |
+
else:
|
97 |
+
res = self.response(get_direct_output(out, self.model, self.api_key, base_url=self.base_url))
|
98 |
+
if not invisible:
|
99 |
+
self.history.append(res.message)
|
100 |
+
return res
|
101 |
+
|
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import GPTSimple as ai
|
5 |
+
import random
|
6 |
+
import vision
|
7 |
+
|
8 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
9 |
+
DESC = "# LLaMA 3.1 Vision\n<p>LLaMA 3.1 Vision uses LLaMA 3.1 405B and Florence 2 to give vision to LLaMA</p>"
|
10 |
+
|
11 |
+
def print_like_dislike(x: gr.LikeData):
|
12 |
+
print(x.index, x.value, x.liked)
|
13 |
+
|
14 |
+
def add_message(history, message):
|
15 |
+
for x in message["files"]:
|
16 |
+
history.append(((x,), None))
|
17 |
+
if message["text"] is not None:
|
18 |
+
history.append((message["text"], None))
|
19 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
20 |
+
|
21 |
+
def bot(history):
|
22 |
+
his = [{"role": "system", "content": "you are a helpful assistant. you can\"see\" image that the user sends by the description being in [IMG][/IMG]. don't reference how you can only see a description"}]
|
23 |
+
nextone = ""
|
24 |
+
for i in history:
|
25 |
+
if isinstance(i[0], tuple):
|
26 |
+
nextone += "[IMG]" + vision.see_file(i[0][0]) + "[/IMG]\n"
|
27 |
+
else:
|
28 |
+
his.append({"role": "user", "content": nextone + i[0]})
|
29 |
+
nextone = ""
|
30 |
+
if i[1] is not None:
|
31 |
+
his.append({"role": "assistant", "content": i[1]})
|
32 |
+
chat = ai.conversation(base_url="deepinfra", model="meta-llama/Meta-Llama-3.1-405B-Instruct", history=his)
|
33 |
+
print(his)
|
34 |
+
stre = chat.generate(stream=True)
|
35 |
+
history[-1][1] = ""
|
36 |
+
for character in stre:
|
37 |
+
if character.token is not None:
|
38 |
+
history[-1][1] += character.token
|
39 |
+
yield history
|
40 |
+
|
41 |
+
def clear_history():
|
42 |
+
return [], {"text":"", "files":[]}
|
43 |
+
|
44 |
+
def retry_last(history):
|
45 |
+
history[-1][1]=None
|
46 |
+
res = bot(history)
|
47 |
+
for i in res:
|
48 |
+
yield i
|
49 |
+
|
50 |
+
with gr.Blocks(fill_height=True, theme=gr.themes.Soft(), css="style.css") as demo:
|
51 |
+
gr.Markdown(DESC)
|
52 |
+
chatbot = gr.Chatbot(
|
53 |
+
elem_id="chatbot",
|
54 |
+
bubble_full_width=False,
|
55 |
+
scale=1,
|
56 |
+
show_label=False
|
57 |
+
)
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
dl = gr.Button("", icon="delete.svg")
|
61 |
+
chat_input = gr.MultimodalTextbox(interactive=True,
|
62 |
+
file_count="multiple",
|
63 |
+
placeholder="Enter message or upload file...", show_label=False)
|
64 |
+
|
65 |
+
re = gr.Button("", icon="retry.svg")
|
66 |
+
|
67 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
68 |
+
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
69 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
70 |
+
|
71 |
+
dl.click(clear_history, None, [chatbot, chat_input])
|
72 |
+
re.click(retry_last, [chatbot], chatbot)
|
73 |
+
|
74 |
+
demo.queue()
|
75 |
+
demo.launch()
|
delete.svg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
gradio_client
|
3 |
+
requests
|
retry.svg
ADDED
style.css
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
textarea {
|
6 |
+
border-radius: 32px;
|
7 |
+
margin-left: 10px;
|
8 |
+
margin-right: 10px
|
9 |
+
}
|
10 |
+
.scroll-hide.svelte-it7283 {
|
11 |
+
padding-top: 12px;
|
12 |
+
}
|
13 |
+
.upload-button.svelte-it7283, .submit-button.svelte-it7283 {
|
14 |
+
border-radius: 32px;
|
15 |
+
min-width: 42px;
|
16 |
+
height: 42px;
|
17 |
+
margin-bottom: 0px;
|
18 |
+
}
|
19 |
+
|
20 |
+
.flex-wrap.user.svelte-1ggj411.svelte-1ggj411 {
|
21 |
+
border-color: var(--color-accent-soft);
|
22 |
+
border-width: 2px;
|
23 |
+
border-radius: 21px;
|
24 |
+
border-bottom-right-radius: 0;
|
25 |
+
padding: 6px 15px;
|
26 |
+
border-color: var(--color-accent-soft);
|
27 |
+
}
|
28 |
+
|
29 |
+
:not(.component-wrap).flex-wrap.bot.svelte-1ggj411.svelte-1ggj411 {
|
30 |
+
border-color: var(--color-accent-soft);
|
31 |
+
border-width: 2px;
|
32 |
+
border-radius: 21px;
|
33 |
+
border-bottom-left-radius: 0;
|
34 |
+
padding: 6px 15px;
|
35 |
+
}
|
36 |
+
|
37 |
+
.lg.svelte-cmf5ev {
|
38 |
+
border-radius: 32px;
|
39 |
+
padding: 0px;
|
40 |
+
max-width: 50px;
|
41 |
+
min-width: 50px;
|
42 |
+
min-height: 50px;
|
43 |
+
max-height: 50px;
|
44 |
+
align-self: center;
|
45 |
+
align-content: center;
|
46 |
+
padding-left: 8px;
|
47 |
+
background-color: var(--block-background-fill);
|
48 |
+
}
|
49 |
+
|
50 |
+
gradio-app .gradio-container.gradio-container-4-39-0 .contain .pending.svelte-1gpwetz {
|
51 |
+
align-self: baseline;
|
52 |
+
background-color: transparent;
|
53 |
+
gap: 2px;
|
54 |
+
width: 25%;
|
55 |
+
height: 42px;
|
56 |
+
border-color: var(--color-accent-soft);
|
57 |
+
border-width: 2px;
|
58 |
+
border-radius: 21px !important;
|
59 |
+
border-bottom-left-radius: 0px !important;
|
60 |
+
margin-left: 29px;
|
61 |
+
padding: 0px;
|
62 |
+
}
|
vision.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client, handle_file
|
2 |
+
cache = {}
|
3 |
+
client = Client("gokaygokay/Florence-2", verbose=False)
|
4 |
+
def see_url(url):
|
5 |
+
result = client.predict(
|
6 |
+
image=handle_file(url),
|
7 |
+
task_prompt="More Detailed Caption",
|
8 |
+
text_input=None,
|
9 |
+
model_id="microsoft/Florence-2-large",
|
10 |
+
api_name="/process_image"
|
11 |
+
)
|
12 |
+
return(result[0].replace("{'<MORE_DETAILED_CAPTION>': '", "").replace("'}", ""))
|
13 |
+
def see_file(fp):
|
14 |
+
if fp in cache:
|
15 |
+
return cache[fp]
|
16 |
+
result = client.predict(
|
17 |
+
image=handle_file(fp),
|
18 |
+
task_prompt="More Detailed Caption",
|
19 |
+
text_input=None,
|
20 |
+
model_id="microsoft/Florence-2-large",
|
21 |
+
api_name="/process_image"
|
22 |
+
)
|
23 |
+
r=result[0].replace("{'<MORE_DETAILED_CAPTION>': '", "").replace("'}", "")
|
24 |
+
cache[fp] = r
|
25 |
+
return r
|