-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.py
155 lines (153 loc) · 3.87 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
import os
import gradio as gr
import requests
from PIL import Image
import base64
import io
import imageio
import json
import socket
url = "http://localhost:8080/completion"
headers = {"Content-Type": "application/json"}
running = False
str = "■"
def run(frame, prompt):
global running
global str
if running:
return
running = True
imageio.imsave('temp.png', frame)
with open("temp.png", 'rb') as file:
encoded_string = base64.b64encode(file.read()).decode('utf-8')
image_data = [{"data": encoded_string, "id": 12}]
data = {"prompt": "USER:[img-12]" + prompt +".\nASSISTANT:", "n_predict": 128, "image_data": image_data, "stream": True}
response = requests.post(url, headers=headers, json=data, stream=True)
with open("output.txt", "a") as write_file:
write_file.write("---"*10 + "\n\n")
for chunk in response.iter_content(chunk_size=128):
with open("output.txt", "a") as write_file:
content = chunk.decode().strip().split('\n\n')[0]
try:
content_split = content.split('data: ')
if len(content_split) > 1:
content_json = json.loads(content_split[1])
write_file.write(content_json["content"])
print(content_json["content"], end='', flush=True)
str = str + content_json["content"]
yield str
write_file.flush() # Save the file after every chunk
except json.JSONDecodeError:
print("JSONDecodeError: Expecting property name enclosed in double quotes")
running = False
str = str + "\n\n■"
css = """
#component-5 {
position: fixed;
top:0;
left:0;
bottom:0;
right:0;
padding: 0 !important;
border-radius: 0 !important;
}
#component-1 {
position: fixed;
bottom: 0;
left: 0;
right: 400px !important;
width: auto !important;
padding: 0;
background: none !important;
}
#component-10 {
z-index:1000;
position: fixed;
top: 0px;
right: 0px;
bottom: 0px;
border-radius: 0 !important;
background: none !important;
border: none !important;
padding: 0 !important;
height: 100% !important;
box-sizing: border-box !important;
width: 400px !important;
}
#component-10 .form {
background: none !important;
border: none !important;
height: 100% !important;
box-sizing: border-box !important;
border-radius: 0 !important;
}
#component-10 .form .container {
height: 100%;
}
#component-2 {
background: none !important;
box-shadow: none !important;
padding: 0 !important;
border: none !important;
height: 100% !important;
box-sizing: border-box !important;
}
.generating {
border: none !important;
}
.upload-container {
width: 100%;
height: 100%;
}
button {
display: none !important;
}
textarea {
background: rgba(0,0,0,0.2) !important;
color: white !important;
font-family: monospace !important;
font-size: 16px !important;
-webkit-text-fill-color: white !important;
border: none !important;
padding: 30px !important;
height: 100% !important;
box-sizing: border-box !important;
border-radius: 0 !important;
}
.progress-text {
background: none !important;;
border: none !important;
color: white !important;
}
video {
height: auto !important;
}
#component-9 {
display: none;
}
[data-testid="block-label"] {
display: none !important;
}
#component-2 [data-testid="block-info"] {
display: none;
}
[data-testid="block-info"] {
padding: 10px !important;
background: rgba(0,0,0,0.2) !important;
display: block;
color: white !important;
margin: 0 !important;
}
"""
demo = gr.Interface(
run,
inputs=[
gr.Image(sources=["webcam"], streaming=True),
gr.Textbox(value="Describe a person in the image", label="Prompt")
],
outputs=gr.Textbox(label="Output Box"),
live=True,
css=css
)
demo.dependencies[0]["show_progress"] = "minimal"
demo.launch()