File size: 4,310 Bytes
e62eac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aade94
e62eac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
064a43c
 
5f84924
e62eac1
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
import gradio as gr
import base64
import requests
import os
import json

MAINTENANCE_NOTICE1 = 'Hint 1: If the app report "Something went wrong, connection error out", please turn off your proxy and retry.\nHint 2: If you upload a large size of image like 10MB, it may take some time to upload and process. Please be patient and wait.'
default_chatbox = []
NOTES = 'This app is adapted from <a href="https://modelscope.cn/models/Fengshenbang/Ziya-Visual-Lyrics-14B">https://modelscope.cn/models/Fengshenbang/Ziya-Visual-Lyrics-14B</a>. It would be recommended to check out the repo if you want to see the detail of our model. And most of the codes attach to this demo are modify from <a href="https://arxiv.org/abs/2312.05278">Lyrics</a>.'

def post(
        input_text,
        image_path,
        temperature,
        top_p,
        result_previous,
        hidden_image
        ):
    # 服务的URL
    with open(image_path,'rb') as f:
        image_prompt = base64.b64encode(f.read())

    service_url = os.getenv('URL_PATH')
    # 准备请求数据
    data = {
        'image_strs': image_prompt,
        'prompt': input_text
    }
        # 发送请求
    print("In the fn")
    response = requests.post(service_url, data=data)
    print(response.json())
    result_text = []
    result_text.append((input_text, response.json()))
    print(result_text)
    return "", result_text, None



def clear_fn(value):
    return "", default_chatbox, None

def clear_fn2(value):
    return default_chatbox

def io_fn(a, b, c):
    print(f"call io_fn")
    return a, b


gr.close_all()
examples = []
with open("./examples/example_inputs.jsonl") as f:
    for line in f:
        data = json.loads(line)
        examples.append(data)

def main():
    with gr.Blocks(css='style.css') as demo:

        with gr.Row():
            with gr.Column(scale=4.5):
                with gr.Group():
                    input_text = gr.Textbox(label='Input Text', placeholder='Please enter text prompt below and press ENTER.')
                    with gr.Row():
                        run_button = gr.Button('Generate')
                        clear_button = gr.Button('Clear')

                    img_path = gr.Image(type="filepath", label="Image Prompt", value=None)
                    
                with gr.Row():
                    temperature = gr.Slider(maximum=1, value=0.7, minimum=0, label='Temperature')
                    top_p = gr.Slider(maximum=1, value=0.1, minimum=0, label='Top P')
                with gr.Group():
                    with gr.Row():
                        with gr.Column(scale=7):    
                            maintenance_notice = gr.Markdown(MAINTENANCE_NOTICE1)
                        with gr.Column(scale=2):    
                            change_button = gr.Button('Change hint to English', visible=False)
            with gr.Column(scale=5.5):
                result_text = gr.Chatbot(label='Multi-round conversation History', value=[])
                hidden_image_hash = gr.Textbox(visible=False)

        gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples], 
                                    inputs=[input_text, img_path],
                                    label="Example Inputs (Click to insert an examplet into the input box)",
                                    examples_per_page=3)
        
        print(gr.__version__)
        run_button.click(fn=post,inputs=[input_text, img_path, temperature, top_p, result_text, hidden_image_hash],
                            outputs=[input_text, result_text, hidden_image_hash])
        input_text.submit(fn=post,inputs=[input_text, img_path, temperature, top_p, result_text, hidden_image_hash],
                            outputs=[input_text, result_text, hidden_image_hash])
        clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, img_path])
        img_path.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
        img_path.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
        
    demo.launch(server_name="0.0.0.0")

if __name__ == '__main__':
    print('start service')
    os.system("pip uninstall -y gradio")
    os.system("pip install gradio==3.29.0")
    print(gr.__version__)
    main()