Smartlizardpy commited on
Commit
65056ae
1 Parent(s): cde5c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -4,17 +4,17 @@ from PIL import Image
4
  from io import BytesIO
5
  from gradio_client import Client
6
  import json
7
- import os
8
- def generate(outline, characters, settings ):
9
- prompt = f"""Hello! I would like to request a 4-paragraph and 700-word per paragraph story and a cover image prompt for sd3 in JSON format described later in the prompt with the following detailed outline:\n\n{outline}\n\nCharacters: {characters}\n\nSettings: {settings}\n\nPlease generate the story with the following detailed JSON format: p1, p2, p3, p4: Keys for story paragraphs; title: Key for story title; prompt: "Mother Nature, a gentle figure surrounded by flowers, stands over a steaming cauldron in a vibrant forest, stirring its contents with a wooden spoon". Please do not include any other text in the output. Thank you. Only the JSON is needed or it will break the whole system and make us lose 10 million dollars. Please don't say 'Full response: Here is the requested output in JSON format:' or 'Here is the full response.' Only JSON. If you give plain text, it will not work and count as an error and we will lose customers. Please do not give text. You are not ChatGPT. Don't say 'Here is the full JSON.' You are not an assistant; you are used by an AI. Thank you.\n\n"""
 
10
 
11
  client = Client("Be-Bo/llama-3-chatbot_70b")
12
  hikaye = client.predict(
13
  message=prompt,
14
  api_name="/chat"
15
  )
16
-
17
- # Debug print to check hikaye
18
  print("Debug: Generated hikaye:", hikaye)
19
 
20
  return hikaye
@@ -39,31 +39,36 @@ def cover(prompts):
39
 
40
  return images
41
 
42
- def parse_story_response(response):
43
  print("Debug: Raw response from API:", response)
44
-
45
  if not response:
46
  print("Debug: Response is empty or None.")
47
- return None, None, None, None, None, None
48
-
49
  title = response.get('title', '')
50
  p1 = response.get('p1', '')
51
  p2 = response.get('p2', '')
52
  p3 = response.get('p3', '')
53
  p4 = response.get('p4', '')
54
- prompt = response.get('prompt', '')
 
55
 
56
- return title, p1, p2, p3, p4, prompt
57
 
 
58
  st.title('Story AI By Ozi')
59
 
60
  characters = st.text_area(label="Characters")
61
  outline = st.text_area(label="Story Outline")
62
  settings = st.text_area(label="Setting")
63
 
 
 
 
64
  if st.button(label="Generate"):
65
  with st.spinner('Generating story and cover images...'):
66
- hikaye = generate(outline, characters, settings)
67
  print("Debug: Story generation response:", hikaye)
68
 
69
  if hikaye:
@@ -73,19 +78,16 @@ if st.button(label="Generate"):
73
  st.error(f"Failed to parse JSON response: {e}")
74
  st.stop()
75
 
76
- title, p1, p2, p3, p4, prmt = parse_story_response(hikaye_json)
77
 
78
  if title and p1 and p2 and p3 and p4:
79
  st.markdown(f'### {title}')
80
 
81
- # Prepare prompts for each paragraph
82
- prompts = [prmt, f"Image for paragraph 1: {p1}", f"Image for paragraph 2: {p2}", f"Image for paragraph 3: {p3}", f"Image for paragraph 4: {p4}"]
83
-
84
  # Generate and display images
85
  images = cover(prompts)
86
  for i, image in enumerate(images):
87
  if image:
88
- st.image(image, caption=prompts[i])
89
  else:
90
  st.error(f"Failed to generate image {i+1}.")
91
 
 
4
  from io import BytesIO
5
  from gradio_client import Client
6
  import json
7
+
8
+ def generate(outline, characters, settings, num_images):
9
+ # Create a dynamic prompt with multiple image prompts based on the number of images
10
+ prompt = f"""Hello! I would like to request a 4-paragraph story (700 words per paragraph) and a corresponding image prompt for each paragraph in JSON format described later in the prompt with the following detailed outline:\n\n{outline}\n\nCharacters: {characters}\n\nSettings: {settings}\n\nPlease generate the story with the following detailed JSON format:\n\np1, p2, p3, p4: Keys for story paragraphs; title: Key for story title; {', '.join([f'prompt{i+1}' for i in range(num_images)])}: Keys for image prompts for each paragraph (or more if additional images are needed). Please do not include any other text in the output. Only JSON is needed or it will break the system and cause loss. Please don't say 'Here is the requested output in JSON format' or anything similar. Thank you."""
11
 
12
  client = Client("Be-Bo/llama-3-chatbot_70b")
13
  hikaye = client.predict(
14
  message=prompt,
15
  api_name="/chat"
16
  )
17
+
 
18
  print("Debug: Generated hikaye:", hikaye)
19
 
20
  return hikaye
 
39
 
40
  return images
41
 
42
+ def parse_story_response(response, num_images):
43
  print("Debug: Raw response from API:", response)
44
+
45
  if not response:
46
  print("Debug: Response is empty or None.")
47
+ return None, None, None, None, []
48
+
49
  title = response.get('title', '')
50
  p1 = response.get('p1', '')
51
  p2 = response.get('p2', '')
52
  p3 = response.get('p3', '')
53
  p4 = response.get('p4', '')
54
+
55
+ prompts = [response.get(f'prompt{i+1}', '') for i in range(num_images)]
56
 
57
+ return title, p1, p2, p3, p4, prompts
58
 
59
+ # Streamlit UI
60
  st.title('Story AI By Ozi')
61
 
62
  characters = st.text_area(label="Characters")
63
  outline = st.text_area(label="Story Outline")
64
  settings = st.text_area(label="Setting")
65
 
66
+ # Number chooser for images (up to 5)
67
+ num_images = st.slider('Number of Images to Generate', 1, 5, 4)
68
+
69
  if st.button(label="Generate"):
70
  with st.spinner('Generating story and cover images...'):
71
+ hikaye = generate(outline, characters, settings, num_images)
72
  print("Debug: Story generation response:", hikaye)
73
 
74
  if hikaye:
 
78
  st.error(f"Failed to parse JSON response: {e}")
79
  st.stop()
80
 
81
+ title, p1, p2, p3, p4, prompts = parse_story_response(hikaye_json, num_images)
82
 
83
  if title and p1 and p2 and p3 and p4:
84
  st.markdown(f'### {title}')
85
 
 
 
 
86
  # Generate and display images
87
  images = cover(prompts)
88
  for i, image in enumerate(images):
89
  if image:
90
+ st.image(image, caption=f"Image {i+1}: {prompts[i]}")
91
  else:
92
  st.error(f"Failed to generate image {i+1}.")
93