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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -1,59 +1,61 @@
1
  import gradio as gr
2
  from gradio_client import Client
3
 
4
- # Connect to your existing 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 fact-based response
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: Generate image and caption
20
  try:
21
- image_caption = image_client.predict(
22
  prompt=topic,
23
  api_name="/generate_image_and_caption"
24
  )
 
 
 
 
25
  except Exception as e:
26
- image_caption = f"Error generating image: {e}"
27
 
28
- # Step 3: Generate narrative continuation
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 with Gemini chat: {e}"
36
 
37
- # Assemble the "book" content
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("## πŸ“– AI-Generated Illustrated Book")
53
- topic = gr.Textbox(label="Enter a topic for your book", placeholder="e.g. Who won the euro 2024?")
54
- output = gr.Textbox(label="Generated Book", lines=20)
55
- button = gr.Button("Generate Book")
56
-
57
- button.click(fn=generate_book, inputs=topic, outputs=output)
 
 
 
 
 
 
 
 
 
 
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()