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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -58
app.py CHANGED
@@ -1,67 +1,43 @@
1
- import gradio as gr
2
  import openai
3
- import requests
4
- from PIL import Image
5
- from io import BytesIO
 
6
 
7
- def infer_character_and_action(api_key, paragraph):
8
- openai.api_key = api_key
 
 
 
9
  response = openai.Completion.create(
10
- model="gpt-3.5-turbo",
11
- prompt=f"From the story paragraph: \"{paragraph}\", extract the main character type and the action they are performing.",
12
- max_tokens=50
13
  )
14
- extracted_info = response.choices[0].text.strip()
15
- character_type, action = extracted_info.split(" and ")
16
- return character_type, action
 
17
 
18
- def create_dalle_prompt(character_type, action):
19
- return f"1800s field-journal style line drawing of {character_type} {action}, very detailed"
 
 
 
20
 
21
- def generate_story_and_images(name, story_title, story_type, api_key):
22
- openai.api_key = api_key
23
- if story_type == "for children":
24
- 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}'."
25
- else:
26
- 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."
27
-
28
- messages = [
29
- {"role": "system", "content": "You are a magical storyteller."},
30
- {"role": "user", "content": prompt_content}
31
- ]
32
-
33
- response = openai.ChatCompletion.create(
34
- model="gpt-3.5-turbo",
35
- messages=messages
36
  )
37
-
38
- story = response.choices[0].message['content']
39
-
40
- # Split story into four paragraphs
41
- paragraphs = story.split('\n')[:4]
42
-
43
- # Extract character type and action from each paragraph and create DALLE prompts
44
- dalle_prompts = []
45
- for para in paragraphs:
46
- character_type, action = infer_character_and_action(api_key, para)
47
- dalle_prompt = create_dalle_prompt(character_type, action)
48
- dalle_prompts.append(dalle_prompt)
49
-
50
- return story, dalle_prompts
51
 
52
- iface = gr.Interface(
53
- fn=generate_story_and_images,
54
- inputs=[
55
- gr.components.Textbox(label="Enter name (Example: Ella)"),
56
- gr.components.Textbox(label="Enter story title (Example: The Enchanted Locket)"),
57
- gr.components.Radio(["for children", "absurd and humorous"], label="Story Type"),
58
- gr.components.Textbox(label="OpenAI API Key", type="password")
59
- ],
60
- outputs=[
61
- "text",
62
- "text"
63
- ],
64
- live=True
65
- )
66
 
67
- iface.launch()
 
 
 
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()