Blessin commited on
Commit
fb0d710
·
1 Parent(s): 19f1bca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -34
app.py CHANGED
@@ -1,43 +1,70 @@
1
- import openai
2
  import gradio as gr
 
 
 
 
 
3
 
4
- # Initialize OpenAI API
5
- openai.api_key = "YOUR_OPENAI_API_KEY"
6
 
7
- def ask_god(question):
8
- # Detailed prompt for God to give humorous or sarcastic replies based on various theologies
9
- prompt = f"Dear God, often portrayed across various theologies and religions with a sense of humor and sarcasm: {question}"
10
-
11
- # Use GPT-3.5 Turbo to get the response
12
- response = openai.Completion.create(
13
- engine="gpt-3.5-turbo",
14
  prompt=prompt,
15
- max_tokens=100
 
16
  )
17
-
18
- # Extract the response text and return
19
- answer = response.choices[0].text.strip()
20
- return answer
21
-
22
- # Gradio Interface
23
- def gradio_interface():
24
- # Define input and output components for Gradio
25
- input_component = gr.inputs.Textbox(lines=5, placeholder="Enter your question to God...")
26
- output_component = gr.outputs.Textbox(label="God's Reply")
27
 
28
- # Create the Gradio interface
29
- iface = gr.Interface(
30
- fn=ask_god,
31
- inputs=input_component,
32
- outputs=output_component,
33
- live=True,
34
- title="Ask God",
35
- description="Get humorous or sarcastic replies from God based on various theologies. Powered by GPT-3.5 Turbo.",
36
- theme="huggingface"
 
 
 
 
 
 
37
  )
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Launch the Gradio interface (for Hugging Face Spaces, use `share=True`)
40
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- if __name__ == "__main__":
43
- gradio_interface()
 
 
1
  import gradio as gr
2
+ import openai
3
+ import requests
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ import textwrap
7
 
8
+ def create_dalle_prompts(story_title, paragraph):
9
+ return f"Illustration of '{story_title}' - {textwrap.shorten(paragraph, width=50)}"
10
 
11
+ def generate_image(api_key, prompt):
12
+ openai.api_key = api_key
13
+ response = openai.Image.create(
 
 
 
 
14
  prompt=prompt,
15
+ n=1,
16
+ size="1024x1024"
17
  )
18
+ image_url = response['data'][0]['url']
19
+ response = requests.get(image_url)
20
+ image = Image.open(BytesIO(response.content))
21
+ return image
 
 
 
 
 
 
22
 
23
+ def generate_story_and_images(name, story_title, story_type, api_key):
24
+ openai.api_key = api_key
25
+ if story_type == "for children":
26
+ prompt_content = f"In a mystical kingdom, a brave adventurer named {name} discovers a magical object. Narrate their whimsical and heartwarming tale based on the title: '{story_title}'."
27
+ else:
28
+ prompt_content = f"In an odd city, {name} stumbles upon a series of bizarre events linked to the title '{story_title}'. Describe their hilariously absurd adventure."
29
+
30
+ messages = [
31
+ {"role": "system", "content": "You are a magical storyteller."},
32
+ {"role": "user", "content": prompt_content}
33
+ ]
34
+
35
+ response = openai.ChatCompletion.create(
36
+ model="gpt-3.5-turbo",
37
+ messages=messages
38
  )
39
+
40
+ story = response.choices[0].message['content']
41
+
42
+ # Split story into four paragraphs
43
+ paragraphs = story.split('\n')[:4]
44
+
45
+ # Create DALLE prompts for each paragraph
46
+ dalle_prompts = [create_dalle_prompts(story_title, para) for para in paragraphs]
47
+
48
+ images = [generate_image(api_key, prompt) for prompt in dalle_prompts]
49
+ return story, dalle_prompts, *images
50
 
51
+ iface = gr.Interface(
52
+ fn=generate_story_and_images,
53
+ inputs=[
54
+ gr.components.Textbox(label="Enter name (Example: Ella)"),
55
+ gr.components.Textbox(label="Enter story title (Example: The Enchanted Locket)"),
56
+ gr.components.Radio(["for children", "absurd and humorous"], label="Story Type"),
57
+ gr.components.Textbox(label="OpenAI API Key", type="password")
58
+ ],
59
+ outputs=[
60
+ "text",
61
+ "text",
62
+ gr.components.Image(label="Image 1", type="pil"),
63
+ gr.components.Image(label="Image 2", type="pil"),
64
+ gr.components.Image(label="Image 3", type="pil"),
65
+ gr.components.Image(label="Image 4", type="pil")
66
+ ],
67
+ live=True
68
+ )
69
 
70
+ iface.launch()