File size: 2,254 Bytes
c06ff17
 
8be8e3e
 
 
c06ff17
8be8e3e
c06ff17
 
 
 
 
8be8e3e
c06ff17
 
 
 
 
 
8be8e3e
c06ff17
8be8e3e
c06ff17
8be8e3e
c06ff17
 
 
8be8e3e
 
 
 
 
 
 
 
 
c06ff17
8be8e3e
 
c06ff17
8be8e3e
c06ff17
8be8e3e
 
c06ff17
 
 
8be8e3e
c06ff17
8be8e3e
 
c06ff17
8be8e3e
 
 
229df10
8be8e3e
 
 
229df10
8be8e3e
 
 
 
229df10
8be8e3e
229df10
 
8be8e3e
229df10
c06ff17
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
from gradio_client import Client
from PIL import Image
import requests
from io import BytesIO

# Connect to remote Gradio apps
grounded_client = Client("admin08077/veo")
image_client = Client("admin08077/picgenai")
chat_client = Client("admin08077/genai")

def generate_book(topic):
    # 1. Grounded response
    try:
        grounded_response = grounded_client.predict(
            prompt=topic,
            api_name="/generate_grounded_response"
        )
    except Exception as e:
        grounded_response = f"Error: {e}"

    # 2. Image and caption
    try:
        caption, image = image_client.predict(
            prompt=topic,
            api_name="/generate_image_and_caption"
        )

        # Image might come back as file path or PIL.Image β€” normalize it
        if isinstance(image, str):  # handle file path
            if image.startswith("http"):
                response = requests.get(image)
                image = Image.open(BytesIO(response.content))
            else:
                image = Image.open(image)

    except Exception as e:
        caption = f"Error generating image: {e}"
        image = None

    # 3. Gemini story
    try:
        narrative = chat_client.predict(
            user_input=f"Write a short story about: {topic}",
            api_name="/chat_with_gemini"
        )
    except Exception as e:
        narrative = f"Error: {e}"

    # Return all parts
    return grounded_response, caption, image, narrative

# Gradio UI
with gr.Blocks(title="πŸ“˜ AI-Generated Mini Book") as demo:
    gr.Markdown("## πŸ“˜ Generate a Mini Illustrated Book with Gemini + Gradio Clients")

    with gr.Row():
        topic_input = gr.Textbox(label="Book Topic", placeholder="e.g. Who won Euro 2024?")
        generate_button = gr.Button("Generate Book")

    grounded_output = gr.Textbox(label="🌍 Grounded Info", lines=3)
    caption_output = gr.Textbox(label="πŸ–ΌοΈ Image Caption")
    image_output = gr.Image(label="πŸ“· Illustration", type="pil")
    story_output = gr.Textbox(label="πŸ“– Story", lines=8)

    generate_button.click(
        fn=generate_book,
        inputs=topic_input,
        outputs=[grounded_output, caption_output, image_output, story_output]
    )

demo.launch()