playground / app.py
alfredplpl's picture
Update app.py
89f67a9
import gradio as gr
import requests
import os
from PIL import Image
from io import BytesIO
import base64
import uuid
os.mkdir("/tmp/gradio")
def respond(message, chat_history):
try:
history_send=[(m,b) for m,b in chat_history if m is not None]
response = requests.post(os.environ["BACKEND"], json={
"data": [
message,
history_send
]
}).json()
#print(response)
data = response["data"]
#print(data)
image=Image.open(BytesIO(base64.b64decode(data[0])), formats=["PNG"])
image_path="/tmp/gradio/"+uuid.uuid4().hex+".png"
image.save(image_path)
chat_history.append((data[2][-1][0],data[2][-1][1]))
chat_history.append((None, (image_path,)))
chat_history.append((None, "ไธŠ่จ˜ใƒ—ใƒญใƒณใƒ—ใƒˆใง็”ปๅƒใ‚’็”Ÿๆˆใ—ใพใ—ใŸใ€‚็”ปๅƒใ‚’ๅค‰ๆ›ดใ—ใŸใ„ๅ ดๅˆใ‚„ๆ–ฐใ—ใไฝœใ‚ŠใŸใ„ๅ ดๅˆใฏๆ•™ใˆใฆใใ ใ•ใ„ใ€‚"))
return "", chat_history
except Exception as e:
print(e)
return "Error",chat_history
css = """
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
"""
title = """
<div style="text-align: center;max-width: 700px;">
<h1>ChatEmi</h1>
<p>ๅ€‹ไบบๆƒ…ๅ ฑใ‚„ๆฉŸๅฏ†ๆƒ…ๅ ฑใ‚’ๅ…ฅๅŠ›ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚</p>
<p>โ€ปใƒ—ใƒญใƒณใƒ—ใƒˆใฎๅ‡บๅŠ›ใŒ่’ใ‚Œใ‚‹ใฎใฏๅ›ฝ็”ฃLLMใฎ้™็•Œใงใ™ใ€‚ไปŠๅพŒใฎๆดป่บใซใ”ๆœŸๅพ…ใใ ใ•ใ„ใ€‚</p>
</div>
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML(title)
chatbot = gr.Chatbot(label='ๅฏพ่ฉฑๅฑฅๆญด', avatar_images=("me.png", "emi.png"))
msg = gr.Textbox(label="ใƒ—ใƒญใƒณใƒ—ใƒˆๆฌ„", placeholder="็”Ÿๆˆใ—ใŸใ„็”ปๅƒใ‚’ๅ…ฅๅŠ›ใ—ใฆใ‚จใƒณใ‚ฟใƒผใ‚ญใƒผใ‚’ๆŠผใ—ใฆใใ ใ•ใ„ใ€‚")
clear = gr.ClearButton([msg, chatbot])
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.queue(concurrency_count=1)
demo.launch()