Spaces:
Sleeping
Sleeping
File size: 1,256 Bytes
9830e11 |
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 |
import os
from io import BytesIO
import gradio as gr
import grpc
from PIL import Image
from inference_pb2 import OmniRequest, OmniResponse
from inference_pb2_grpc import OmniServiceStub
#from utils.shape_predictor import align_face
def get_bytes(img):
if img is None:
return img
buffered = BytesIO()
img.save(buffered, format="JPEG")
return buffered.getvalue()
def bytes_to_image(image: bytes) -> Image.Image:
image = Image.open(BytesIO(image))
return image
def generate_answer(question, image):
image_bytes = get_bytes(image)
if image_bytes is None:
image_bytes = b'image'
with grpc.insecure_channel(os.environ['SERVER']) as channel:
stub = OmniServiceStub(channel)
output: OmniResponse = stub.get_answer(OmniRequest(image=image_bytes, question=question))
output = output.answer
#output = bytes_to_image(output.image)
return output
def get_demo():
demo = gr.Interface(
fn=generate_answer,
inputs=["text", gr.Image(type="pil")],
outputs=["text"],
)
return demo
if __name__ == '__main__':
#align_cache = LRUCache(maxsize=10)
demo = get_demo()
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|