lu junyu commited on
Commit
e62eac1
1 Parent(s): 00932e2
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+ import requests
4
+ import os
5
+ import json
6
+
7
+ 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.'
8
+ default_chatbox = []
9
+ 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>.'
10
+
11
+ def post(
12
+ input_text,
13
+ image_path,
14
+ temperature,
15
+ top_p,
16
+ result_previous,
17
+ hidden_image
18
+ ):
19
+ # 服务的URL
20
+ with open(image_path,'rb') as f:
21
+ image_prompt = base64.b64encode(f.read())
22
+
23
+ service_url = os.getenv('URL_PATH')
24
+ # 准备请求数据
25
+ data = {
26
+ 'image_strs': image_prompt,
27
+ 'prompt': input_text
28
+ }
29
+ # 发送请求
30
+ print("In the fn")
31
+ response = requests.post(service_url, data=data)
32
+ print(response.json())
33
+ result_text = []
34
+ result_text.append((input_text, response.json()))
35
+ print(result_text)
36
+ return "", result_text, None
37
+
38
+
39
+
40
+ def clear_fn(value):
41
+ return "", default_chatbox, None
42
+
43
+ def clear_fn2(value):
44
+ return default_chatbox
45
+
46
+ def io_fn(a, b, c):
47
+ print(f"call io_fn")
48
+ return a, b
49
+
50
+
51
+ gr.close_all()
52
+ examples = []
53
+ with open("./examples/example_inputs.jsonl") as f:
54
+ for line in f:
55
+ data = json.loads(line)
56
+ examples.append(data)
57
+
58
+ def main():
59
+ with gr.Blocks(css='style.css') as demo:
60
+
61
+ with gr.Row():
62
+ with gr.Column(scale=4.5):
63
+ with gr.Group():
64
+ input_text = gr.Textbox(label='Input Text', placeholder='Please enter text prompt below and press ENTER.')
65
+ with gr.Row():
66
+ run_button = gr.Button('Generate')
67
+ clear_button = gr.Button('Clear')
68
+
69
+ img_path = gr.Image(type="filepath", label="Image Prompt", value=None)
70
+
71
+ with gr.Row():
72
+ temperature = gr.Slider(maximum=1, value=0.7, minimum=0, label='Temperature')
73
+ top_p = gr.Slider(maximum=1, value=0.1, minimum=0, label='Top P')
74
+ with gr.Group():
75
+ with gr.Row():
76
+ with gr.Column(scale=7):
77
+ maintenance_notice = gr.Markdown(MAINTENANCE_NOTICE1)
78
+ with gr.Column(scale=2):
79
+ change_button = gr.Button('Change hint to English', visible=False)
80
+ with gr.Column(scale=5.5):
81
+ result_text = gr.components.Chatbot(label='Multi-round conversation History', value=[]).style(height=550)
82
+ hidden_image_hash = gr.Textbox(visible=False)
83
+
84
+ gr_examples = gr.Examples(examples=[[example["text"], example["image"]] for example in examples],
85
+ inputs=[input_text, img_path],
86
+ label="Example Inputs (Click to insert an examplet into the input box)",
87
+ examples_per_page=3)
88
+
89
+ print(gr.__version__)
90
+ run_button.click(fn=post,inputs=[input_text, img_path, temperature, top_p, result_text, hidden_image_hash],
91
+ outputs=[input_text, result_text, hidden_image_hash])
92
+ input_text.submit(fn=post,inputs=[input_text, img_path, temperature, top_p, result_text, hidden_image_hash],
93
+ outputs=[input_text, result_text, hidden_image_hash])
94
+ clear_button.click(fn=clear_fn, inputs=clear_button, outputs=[input_text, result_text, img_path])
95
+ img_path.upload(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
96
+ img_path.clear(fn=clear_fn2, inputs=clear_button, outputs=[result_text])
97
+
98
+ demo.queue(concurrency_count=10)
99
+ demo.launch(server_name="0.0.0.0")
100
+
101
+ if __name__ == '__main__':
102
+ print('start service')
103
+ main()
examples/1.png ADDED
examples/2.jpeg ADDED
examples/3.jpg ADDED
examples/example_inputs.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {"id":1, "text": "Which country's national emblem is shown in the picture? Please give a brief description of its history.", "image": "examples/1.png"}
2
+ {"id":1, "text": "Answer the question with short sentence. Who is the author of this picture?", "image": "examples/2.jpeg"}
3
+ {"id":1, "text": "Who is the man in the picture? Please give a brief introduction about his life.", "image": "examples/3.jpg"}
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pydantic==1.10.11