ysharma HF Staff commited on
Commit
0844d45
·
1 Parent(s): 4e7e062

create app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -0
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ from PIL import Image
4
+ from bs4 import BeautifulSoup
5
+
6
+ def whisper_stt(audio):
7
+ print("inside whisper_stt")
8
+ print(f'audio is - {audio}')
9
+ print(f'audio is - {type(audio)}')
10
+ #Connect to the Space
11
+ client = Client("abidlabs/whisper", hf_token="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")
12
+ print ("now here")
13
+ text = client.predict(audio, api_name = '/predict')
14
+ print(f"text is - {text}")
15
+ return text
16
+
17
+ def ttimg(prompt):
18
+ print("inside ttimg")
19
+ #Connect to the Space
20
+ client = Client("stabilityai/stable-diffusion")
21
+ img_dir = client.predict(prompt,"blur",7, fn_index = 1)
22
+ print(f'image dir - {img_dir}')
23
+ jpg_files = [f for f in os.listdir(img_dir) if f.endswith('.jpg')]
24
+ #if len(jpg_files) > 0:
25
+ # chosen_file = random.choice(jpg_files)
26
+ file_path = os.path.join(img_dir, jpg_files[0])
27
+ print(f'file_path - {file_path}')
28
+ return file_path
29
+ #with open(file_path, 'rb') as f:
30
+ # pil_img = Image.open(f)
31
+ # return pil_img.show()
32
+
33
+ def controlnet_img2img(img, cnet_prompt):
34
+ print("inside controlnet_img2img")
35
+ #Connect to the Space
36
+ client = Client("ysharma/ControlNetwithSlider")
37
+ img_dir = client.predict(img, cnet_prompt, "", "blur",1,512,20,9,123,0,100,200,api_name = '/canny')
38
+ png_files = [os.path.join(img_dir, f) for f in os.listdir(img_dir) if f.endswith('.png')]
39
+ return png_files[0], png_files[1]
40
+
41
+ def img2para(cnet_img2):
42
+ print("inside img2para")
43
+ #Connect to the Space
44
+ client = Client("Awiny/Image2Paragraph")
45
+ txt = client.predict(cnet_img2, [], api_name="/predict")
46
+ print(f'img2para text is - {txt}')
47
+ soup = BeautifulSoup(txt, 'html.parser')
48
+ gpt4_section = soup.find_all('div', {'style': 'display: flex; flex-wrap: wrap;'})[1].find_all('p')[0].text
49
+ return gpt4_section
50
+
51
+
52
+ # demo with different Gradio Clients
53
+ demo = gr.Blocks()
54
+
55
+ with demo:
56
+ with gr.Row():
57
+ audio_mic = gr.Audio(source="upload", type="filepath")
58
+ with gr.Column():
59
+ btn_whisper = gr.Button("Transcribe👇").style(full_width=True)
60
+ prompt = gr.Textbox(label="prompt for SD image generation", interactive=False) #, value='a black swan')
61
+ with gr.Row():
62
+ btn_sd = gr.Button("Generate Image👉").style(full_width=True)
63
+ sd_img = gr.Image(type='filepath') #value="/content/e321040c-b2eb-4550-8aaa-4cd416a7602d/tmpwyiwdr_1.jpg",
64
+ with gr.Row():
65
+ with gr.Column():
66
+ cnet_prompt = gr.Textbox(label="prompt for SD image generation")
67
+ btn_cnet = gr.Button("ControlNet Output👉").style(full_width=True)
68
+ cnet_img1 = gr.Image(type='filepath')
69
+ cnet_img2 = gr.Image(type='filepath')
70
+ with gr.Row():
71
+ btn_Img2para = gr.Button("Lets understand Image with Text👉").style(full_width=True)
72
+ paragraph = gr.Textbox(label="Image to Paragraph generation")
73
+
74
+ btn_whisper.click(whisper_stt, audio_mic, prompt)
75
+ btn_sd.click(ttimg, prompt, sd_img)
76
+ btn_cnet.click(controlnet_img2img, [sd_img, cnet_prompt], [cnet_img1, cnet_img2])
77
+ btn_Img2para.click(img2para, cnet_img1, paragraph)
78
+
79
+ #demo.launch()
80
+
81
+ # demo with different Gradio Clients Chained together
82
+ demo_chain_events = gr.Blocks()
83
+
84
+ with demo_chain_events:
85
+ #with gr.Row():
86
+ audio_mic = gr.Audio(source="upload", type="filepath")
87
+ btn_whisper = gr.Button("Trigger the Chained Events🚀🚀").style(full_width=True)
88
+ prompt = gr.Textbox(label="Getting the Prompt for Stable Diffusion using Whisper Gradio Client", interactive=False,) #value='a black swan'
89
+
90
+ with gr.Row():
91
+ with gr.Column():
92
+ html_sd = gr.HTML("<h1><center>Stable Diffusion Generated Image👇</center></h1>") #.style(full_width=True)
93
+ sd_img = gr.Image(type='filepath') #value="/content/e321040c-b2eb-4550-8aaa-4cd416a7602d/tmpwyiwdr_1.jpg",
94
+ #with gr.Row():
95
+ with gr.Column():
96
+ with gr.Row():
97
+ html_cnet = gr.HTML("<h1><center>ControlNet Output👇</center></h1>") #.style(full_width=True)
98
+ cnet_prompt = gr.Textbox(label="Prompt for ControlNet", value='a beautiful scenery')
99
+ with gr.Row():
100
+ cnet_img1 = gr.Image(type='filepath')
101
+ cnet_img2 = gr.Image(type='filepath')
102
+ with gr.Row():
103
+ with gr.Column():
104
+ html_Img2para = gr.HTML("<h1><center>Image to Paragraph output👇</center></h1>") #.style(full_width=True)
105
+ paragraph = gr.Textbox(label="Image to Paragraph generation")
106
+
107
+ """
108
+ btn_whisper.click(whisper_stt, audio_mic, prompt)
109
+ btn_sd.click(ttimg, prompt, sd_img)
110
+ btn_cnet.click(controlnet_img2img, [sd_img, cnet_prompt], [cnet_img1, cnet_img2])
111
+ btn_Img2para.click(img2para, cnet_img1, paragraph)
112
+ """
113
+ btn_whisper.click(whisper_stt, audio_mic, prompt)\
114
+ .success(ttimg, prompt, sd_img)\
115
+ .success(controlnet_img2img, [sd_img, cnet_prompt], [cnet_img1, cnet_img2])\
116
+ .success(img2para, cnet_img1, paragraph)
117
+
118
+ #demo_chain_events.launch()
119
+
120
+
121
+ demo_joint = gr.Blocks()
122
+ with demo_joint:
123
+ with gr.Row():
124
+ with gr.Box():
125
+ demo.render()
126
+ with gr.Box():
127
+ demo_chain_events.render()
128
+
129
+
130
+ demo_joint.launch(debug=True)