Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Connect to
|
| 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 |
-
#
|
| 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"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
prompt=topic,
|
| 23 |
api_name="/generate_image_and_caption"
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
user_input=f"Write a short story
|
| 36 |
api_name="/chat_with_gemini"
|
| 37 |
)
|
| 38 |
except Exception as e:
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
narrative_output = gr.Textbox(label="βοΈ Chapter 3: Story", lines=10)
|
| 54 |
|
| 55 |
-
|
| 56 |
fn=generate_book,
|
| 57 |
inputs=topic_input,
|
| 58 |
-
outputs=[grounded_output,
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_client import Client
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
+
# Connect to remote Gradio apps
|
| 8 |
grounded_client = Client("admin08077/veo")
|
| 9 |
image_client = Client("admin08077/picgenai")
|
| 10 |
chat_client = Client("admin08077/genai")
|
| 11 |
|
| 12 |
def generate_book(topic):
|
| 13 |
+
# 1. Grounded response
|
| 14 |
try:
|
| 15 |
grounded_response = grounded_client.predict(
|
| 16 |
prompt=topic,
|
| 17 |
api_name="/generate_grounded_response"
|
| 18 |
)
|
| 19 |
except Exception as e:
|
| 20 |
+
grounded_response = f"Error: {e}"
|
| 21 |
|
| 22 |
+
# 2. Image and caption
|
| 23 |
try:
|
| 24 |
+
caption, image = image_client.predict(
|
| 25 |
prompt=topic,
|
| 26 |
api_name="/generate_image_and_caption"
|
| 27 |
)
|
| 28 |
+
|
| 29 |
+
# Image might come back as file path or PIL.Image β normalize it
|
| 30 |
+
if isinstance(image, str): # handle file path
|
| 31 |
+
if image.startswith("http"):
|
| 32 |
+
response = requests.get(image)
|
| 33 |
+
image = Image.open(BytesIO(response.content))
|
| 34 |
+
else:
|
| 35 |
+
image = Image.open(image)
|
| 36 |
+
|
| 37 |
except Exception as e:
|
| 38 |
+
caption = f"Error generating image: {e}"
|
| 39 |
+
image = None
|
| 40 |
|
| 41 |
+
# 3. Gemini story
|
| 42 |
try:
|
| 43 |
+
narrative = chat_client.predict(
|
| 44 |
+
user_input=f"Write a short story about: {topic}",
|
| 45 |
api_name="/chat_with_gemini"
|
| 46 |
)
|
| 47 |
except Exception as e:
|
| 48 |
+
narrative = f"Error: {e}"
|
| 49 |
|
| 50 |
+
# Return all parts
|
| 51 |
+
return grounded_response, caption, image, narrative
|
| 52 |
|
| 53 |
+
# Gradio UI
|
| 54 |
+
with gr.Blocks(title="π AI-Generated Mini Book") as demo:
|
| 55 |
+
gr.Markdown("## π Generate a Mini Illustrated Book with Gemini + Gradio Clients")
|
| 56 |
|
| 57 |
+
with gr.Row():
|
| 58 |
+
topic_input = gr.Textbox(label="Book Topic", placeholder="e.g. Who won Euro 2024?")
|
| 59 |
+
generate_button = gr.Button("Generate Book")
|
| 60 |
|
| 61 |
+
grounded_output = gr.Textbox(label="π Grounded Info", lines=3)
|
| 62 |
+
caption_output = gr.Textbox(label="πΌοΈ Image Caption")
|
| 63 |
+
image_output = gr.Image(label="π· Illustration", type="pil")
|
| 64 |
+
story_output = gr.Textbox(label="π Story", lines=8)
|
|
|
|
| 65 |
|
| 66 |
+
generate_button.click(
|
| 67 |
fn=generate_book,
|
| 68 |
inputs=topic_input,
|
| 69 |
+
outputs=[grounded_output, caption_output, image_output, story_output]
|
| 70 |
)
|
| 71 |
|
| 72 |
demo.launch()
|