Smartlizardpy commited on
Commit
cde5c47
·
verified ·
1 Parent(s): 6b5aec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -19,21 +19,25 @@ def generate(outline, characters, settings ):
19
 
20
  return hikaye
21
 
22
- def cover(prompt):
23
  api_key = st.secrets['apikey']
24
  model = "mann-e/Mann-E_Turbo"
25
  headers = {"Authorization": f"Bearer {api_key}"}
26
  api_url = f"https://api-inference.huggingface.co/models/{model}"
27
-
28
- data = {"inputs": prompt}
29
- response = requests.post(api_url, headers=headers, json=data)
30
-
31
- if 'image' in response.headers.get('content-type', '').lower():
32
- image = Image.open(BytesIO(response.content))
33
- return image
34
- else:
35
- st.error("Failed to fetch cover image.")
36
- return None
 
 
 
 
37
 
38
  def parse_story_response(response):
39
  print("Debug: Raw response from API:", response)
@@ -58,16 +62,15 @@ outline = st.text_area(label="Story Outline")
58
  settings = st.text_area(label="Setting")
59
 
60
  if st.button(label="Generate"):
61
- with st.spinner('Generating story and cover image...'):
62
- hikaye = generate(outline, characters, settings )
63
  print("Debug: Story generation response:", hikaye)
64
-
65
  if hikaye:
66
  try:
67
  hikaye_json = json.loads(hikaye)
68
  except json.JSONDecodeError as e:
69
  st.error(f"Failed to parse JSON response: {e}")
70
- st.error(f"Failed to parse JSON response.{hikaye}")
71
  st.stop()
72
 
73
  title, p1, p2, p3, p4, prmt = parse_story_response(hikaye_json)
@@ -75,27 +78,26 @@ if st.button(label="Generate"):
75
  if title and p1 and p2 and p3 and p4:
76
  st.markdown(f'### {title}')
77
 
78
- # Display cover image if available
79
- image = cover(prmt)
80
- if image:
81
-
82
- st.image(image ,caption= prmt)
83
- else:
84
- st.error("Failed to generate story cover.")
 
 
 
85
 
86
  # Display paragraphs
87
  st.markdown(f'''
88
  {p1}
89
-
90
  {p2}
91
-
92
  {p3}
93
-
94
  {p4}
95
  ''')
96
 
97
  else:
98
  st.error("Failed to generate or parse story.")
99
-
100
  else:
101
  st.error("No story data received.")
 
19
 
20
  return hikaye
21
 
22
+ def cover(prompts):
23
  api_key = st.secrets['apikey']
24
  model = "mann-e/Mann-E_Turbo"
25
  headers = {"Authorization": f"Bearer {api_key}"}
26
  api_url = f"https://api-inference.huggingface.co/models/{model}"
27
+
28
+ images = []
29
+ for prompt in prompts:
30
+ data = {"inputs": prompt}
31
+ response = requests.post(api_url, headers=headers, json=data)
32
+
33
+ if 'image' in response.headers.get('content-type', '').lower():
34
+ image = Image.open(BytesIO(response.content))
35
+ images.append(image)
36
+ else:
37
+ st.error(f"Failed to fetch image for prompt: {prompt}")
38
+ images.append(None)
39
+
40
+ return images
41
 
42
  def parse_story_response(response):
43
  print("Debug: Raw response from API:", response)
 
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:
70
  try:
71
  hikaye_json = json.loads(hikaye)
72
  except json.JSONDecodeError as e:
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)
 
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
 
92
  # Display paragraphs
93
  st.markdown(f'''
94
  {p1}
 
95
  {p2}
 
96
  {p3}
 
97
  {p4}
98
  ''')
99
 
100
  else:
101
  st.error("Failed to generate or parse story.")
 
102
  else:
103
  st.error("No story data received.")