philipp-zettl's picture
Upload app.py
3dcceab verified
raw
history blame
2.19 kB
import os
import shutil
import gradio as gr
from uuid import uuid4
from requests import Session
import urllib.parse
class Client:
def __init__(self):
self.c = Session()
self.c.headers.update({
'Content-Type': 'application/json',
'Authorization': 'Bearer b86b299e-c904-4c99-9357-044a69bdb7ab'
})
def send_message(self, input):
res = self.c.post(
'https://api-controller.dev.easybits.tech/api/852',
json={'message': {'text': input}}
)
return res.json()
def send_images(self, images, text):
res = self.c.post(
'https://api-controller.dev.easybits.tech/api/852',
files={'incoming_files': images, 'text': text}
)
return res.json()
client = Client()
def send_message(input, files):
print('sending message', files)
res = client.send_images(files, input)
print(res)
return res['message']['data']
def upload_files(files):
if not os.path.exists('uploads'):
os.makedirs('uploads')
print('uploading files', files)
button = None
file_urls = []
for file in files:
file_urls.append(
f'http://localhost:7860/gradio_api/file={urllib.parse.quote_plus(file)}'
)
print(file_urls)
return file_urls, button
with gr.Blocks() as demo:
gr.Markdown("## Demo")
with gr.Row(equal_height=True):
with gr.Column():
gr.Markdown("### Input")
input = gr.Textbox(label="LoRA Key-Word")
upload_button = gr.UploadButton("Upload Images", file_count='multiple', file_types=['image'])
hidden_files = gr.State([])
download_button = gr.DownloadButton("Download", visible=False)
upload_button.upload(upload_files, upload_button, [hidden_files, download_button])
with gr.Column():
gr.Markdown("### Output")
output = gr.Textbox(lines=5, label="Output Text")
with gr.Row():
send_button = gr.Button("Send")
send_button.click(send_message, inputs=[input, hidden_files], outputs=[output])
demo.launch(share=True, allowed_paths=['uploads/*'])