Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -2,63 +2,45 @@ import gradio as gr
|
|
2 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
3 |
import re
|
4 |
import os
|
|
|
5 |
hf_token = os.environ.get('HF_TOKEN')
|
6 |
from gradio_client import Client
|
7 |
client = Client("https://fffiloni-test-llama-api-debug.hf.space/", hf_token=hf_token)
|
8 |
-
|
9 |
clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
|
10 |
|
11 |
def get_text_after_colon(input_text):
|
12 |
-
# Find the first occurrence of ":"
|
13 |
colon_index = input_text.find(":")
|
14 |
-
|
15 |
-
# Check if ":" exists in the input_text
|
16 |
if colon_index != -1:
|
17 |
-
# Extract the text after the colon
|
18 |
result_text = input_text[colon_index + 1:].strip()
|
19 |
return result_text
|
20 |
else:
|
21 |
-
# Return the original text if ":" is not found
|
22 |
return input_text
|
23 |
|
24 |
-
def infer(image_input, audience):
|
25 |
gr.Info('Calling CLIP Interrogator ...')
|
26 |
clipi_result = clipi_client.predict(
|
27 |
-
image_input,
|
28 |
-
"best",
|
29 |
-
4,
|
30 |
api_name="/clipi2"
|
31 |
)
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
llama_q = f""
|
36 |
-
|
37 |
-
|
38 |
-
'{clipi_result[0]}'
|
39 |
-
|
40 |
-
"""
|
41 |
gr.Info('Calling Llama2 ...')
|
42 |
result = client.predict(
|
43 |
-
llama_q,
|
44 |
"I2S",
|
45 |
api_name="/predict"
|
46 |
)
|
47 |
-
|
48 |
-
print(f"Llama2 result: {result}")
|
49 |
-
|
50 |
result = get_text_after_colon(result)
|
51 |
-
|
52 |
-
# Split the text into paragraphs based on actual line breaks
|
53 |
paragraphs = result.split('\n')
|
54 |
-
|
55 |
-
# Join the paragraphs back with an extra empty line between each paragraph
|
56 |
formatted_text = '\n\n'.join(paragraphs)
|
57 |
-
|
58 |
-
|
59 |
return formatted_text, gr.Group.update(visible=True)
|
60 |
|
61 |
-
css="""
|
62 |
#col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
|
63 |
a {text-decoration-line: underline; font-weight: 600;}
|
64 |
a {text-decoration-line: underline; font-weight: 600;}
|
@@ -129,11 +111,11 @@ with gr.Blocks(css=css) as demo:
|
|
129 |
with gr.Column():
|
130 |
image_in = gr.Image(label="Image input", type="filepath", elem_id="image-in", height=420)
|
131 |
audience = gr.Radio(label="Target Audience", choices=["Children", "Adult"], value="Children")
|
|
|
|
|
132 |
submit_btn = gr.Button('Tell me a story')
|
133 |
with gr.Column():
|
134 |
-
|
135 |
-
story = gr.Textbox(label="generated Story", elem_id="story")
|
136 |
-
|
137 |
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
138 |
community_icon = gr.HTML(community_icon_html)
|
139 |
loading_icon = gr.HTML(loading_icon_html)
|
@@ -141,12 +123,9 @@ with gr.Blocks(css=css) as demo:
|
|
141 |
|
142 |
gr.Examples(examples=[["./examples/crabby.png", "Children"],["./examples/hopper.jpeg", "Adult"]],
|
143 |
fn=infer,
|
144 |
-
inputs=[image_in, audience],
|
145 |
outputs=[story, share_group],
|
146 |
cache_examples=True
|
147 |
)
|
148 |
|
149 |
-
submit_btn.click(fn=infer, inputs=[image_in, audience
|
150 |
-
share_button.click(None, [], [], _js=share_js)
|
151 |
-
|
152 |
-
demo.queue(max_size=12).launch()
|
|
|
2 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
3 |
import re
|
4 |
import os
|
5 |
+
|
6 |
hf_token = os.environ.get('HF_TOKEN')
|
7 |
from gradio_client import Client
|
8 |
client = Client("https://fffiloni-test-llama-api-debug.hf.space/", hf_token=hf_token)
|
|
|
9 |
clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
|
10 |
|
11 |
def get_text_after_colon(input_text):
|
|
|
12 |
colon_index = input_text.find(":")
|
|
|
|
|
13 |
if colon_index != -1:
|
|
|
14 |
result_text = input_text[colon_index + 1:].strip()
|
15 |
return result_text
|
16 |
else:
|
|
|
17 |
return input_text
|
18 |
|
19 |
+
def infer(image_input, audience, clip_caption, llama_prompt):
|
20 |
gr.Info('Calling CLIP Interrogator ...')
|
21 |
clipi_result = clipi_client.predict(
|
22 |
+
image_input,
|
23 |
+
"best",
|
24 |
+
4,
|
25 |
api_name="/clipi2"
|
26 |
)
|
27 |
+
clip_caption.value = clipi_result[0]
|
|
|
28 |
|
29 |
+
llama_q = f"{llama_prompt} " + \
|
30 |
+
f"Here's the image description: '{clip_caption.value}'"
|
31 |
+
|
|
|
|
|
|
|
32 |
gr.Info('Calling Llama2 ...')
|
33 |
result = client.predict(
|
34 |
+
llama_q,
|
35 |
"I2S",
|
36 |
api_name="/predict"
|
37 |
)
|
|
|
|
|
|
|
38 |
result = get_text_after_colon(result)
|
|
|
|
|
39 |
paragraphs = result.split('\n')
|
|
|
|
|
40 |
formatted_text = '\n\n'.join(paragraphs)
|
|
|
|
|
41 |
return formatted_text, gr.Group.update(visible=True)
|
42 |
|
43 |
+
css = """
|
44 |
#col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
|
45 |
a {text-decoration-line: underline; font-weight: 600;}
|
46 |
a {text-decoration-line: underline; font-weight: 600;}
|
|
|
111 |
with gr.Column():
|
112 |
image_in = gr.Image(label="Image input", type="filepath", elem_id="image-in", height=420)
|
113 |
audience = gr.Radio(label="Target Audience", choices=["Children", "Adult"], value="Children")
|
114 |
+
clip_caption = gr.Textbox(label="CLIP Generated Caption", default="")
|
115 |
+
llama_prompt = gr.Textbox(label="Llama2 Prompt", default="I'll give you a simple image caption, please provide a fictional story for a {audience} audience that would fit well with the image. Please be creative, do not worry and only generate a cool fictional story.")
|
116 |
submit_btn = gr.Button('Tell me a story')
|
117 |
with gr.Column():
|
118 |
+
story = gr.Textbox(label="Generated Story", elem_id="story")
|
|
|
|
|
119 |
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
120 |
community_icon = gr.HTML(community_icon_html)
|
121 |
loading_icon = gr.HTML(loading_icon_html)
|
|
|
123 |
|
124 |
gr.Examples(examples=[["./examples/crabby.png", "Children"],["./examples/hopper.jpeg", "Adult"]],
|
125 |
fn=infer,
|
126 |
+
inputs=[image_in, audience, clip_caption, llama_prompt],
|
127 |
outputs=[story, share_group],
|
128 |
cache_examples=True
|
129 |
)
|
130 |
|
131 |
+
submit_btn.click(fn=infer, inputs=[image_in, audience, clip_caption, llama_prompt], outputs
|
|
|
|
|
|