admin08077 commited on
Commit
8be8e3e
Β·
verified Β·
1 Parent(s): 229df10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -1,61 +1,72 @@
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()
 
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()