Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
| 3 |
|
| 4 |
-
# Connect to your
|
| 5 |
grounded_client = Client("admin08077/veo")
|
| 6 |
image_client = Client("admin08077/picgenai")
|
| 7 |
chat_client = Client("admin08077/genai")
|
| 8 |
|
| 9 |
def generate_book(topic):
|
| 10 |
-
# Step 1: Get grounded
|
| 11 |
try:
|
| 12 |
grounded_response = grounded_client.predict(
|
| 13 |
prompt=topic,
|
| 14 |
api_name="/generate_grounded_response"
|
| 15 |
)
|
| 16 |
except Exception as e:
|
| 17 |
-
grounded_response = f"Error getting grounded response: {e}"
|
| 18 |
|
| 19 |
-
# Step 2:
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
prompt=topic,
|
| 23 |
api_name="/generate_image_and_caption"
|
| 24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
|
| 27 |
|
| 28 |
-
# Step 3:
|
| 29 |
try:
|
| 30 |
gemini_narrative = chat_client.predict(
|
| 31 |
user_input=f"Write a short story based on: {topic}",
|
| 32 |
api_name="/chat_with_gemini"
|
| 33 |
)
|
| 34 |
except Exception as e:
|
| 35 |
-
gemini_narrative = f"Error
|
| 36 |
|
| 37 |
-
|
| 38 |
-
book_text = f"""# Illustrated Book: "{topic.title()}"
|
| 39 |
-
|
| 40 |
-
## Chapter 1: The Facts
|
| 41 |
-
{grounded_response}
|
| 42 |
-
|
| 43 |
-
## Chapter 2: The Picture
|
| 44 |
-
πΌοΈ {image_caption}
|
| 45 |
-
|
| 46 |
-
## Chapter 3: The Story
|
| 47 |
-
{gemini_narrative}
|
| 48 |
-
"""
|
| 49 |
-
return book_text
|
| 50 |
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("##
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
| 3 |
|
| 4 |
+
# Connect to your deployed Gradio apps
|
| 5 |
grounded_client = Client("admin08077/veo")
|
| 6 |
image_client = Client("admin08077/picgenai")
|
| 7 |
chat_client = Client("admin08077/genai")
|
| 8 |
|
| 9 |
def generate_book(topic):
|
| 10 |
+
# Step 1: Get grounded info
|
| 11 |
try:
|
| 12 |
grounded_response = grounded_client.predict(
|
| 13 |
prompt=topic,
|
| 14 |
api_name="/generate_grounded_response"
|
| 15 |
)
|
| 16 |
except Exception as e:
|
| 17 |
+
grounded_response = f"β Error getting grounded response: {e}"
|
| 18 |
|
| 19 |
+
# Step 2: Get image + caption
|
| 20 |
try:
|
| 21 |
+
result = image_client.predict(
|
| 22 |
prompt=topic,
|
| 23 |
api_name="/generate_image_and_caption"
|
| 24 |
)
|
| 25 |
+
if isinstance(result, (list, tuple)) and len(result) == 2:
|
| 26 |
+
image_url_or_path, caption = result
|
| 27 |
+
else:
|
| 28 |
+
image_url_or_path, caption = None, "β οΈ Unexpected image format."
|
| 29 |
except Exception as e:
|
| 30 |
+
image_url_or_path, caption = None, f"β Error generating image: {e}"
|
| 31 |
|
| 32 |
+
# Step 3: Get story content
|
| 33 |
try:
|
| 34 |
gemini_narrative = chat_client.predict(
|
| 35 |
user_input=f"Write a short story based on: {topic}",
|
| 36 |
api_name="/chat_with_gemini"
|
| 37 |
)
|
| 38 |
except Exception as e:
|
| 39 |
+
gemini_narrative = f"β Error generating narrative: {e}"
|
| 40 |
|
| 41 |
+
return grounded_response, image_url_or_path, caption, gemini_narrative
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("## π AI-Generated Illustrated Book")
|
| 45 |
+
|
| 46 |
+
topic_input = gr.Textbox(label="Book Topic", placeholder="e.g. Who won Euro 2024?")
|
| 47 |
+
generate_btn = gr.Button("π Generate Book")
|
| 48 |
+
|
| 49 |
+
with gr.Column():
|
| 50 |
+
grounded_output = gr.Textbox(label="π Chapter 1: Grounded Facts", lines=4)
|
| 51 |
+
image_output = gr.Image(label="πΌοΈ Chapter 2: Illustration")
|
| 52 |
+
caption_output = gr.Textbox(label="Image Caption")
|
| 53 |
+
narrative_output = gr.Textbox(label="βοΈ Chapter 3: Story", lines=10)
|
| 54 |
+
|
| 55 |
+
generate_btn.click(
|
| 56 |
+
fn=generate_book,
|
| 57 |
+
inputs=topic_input,
|
| 58 |
+
outputs=[grounded_output, image_output, caption_output, narrative_output]
|
| 59 |
+
)
|
| 60 |
|
| 61 |
demo.launch()
|