Blessin commited on
Commit
c8e6137
·
1 Parent(s): c19d8a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -45
app.py CHANGED
@@ -1,55 +1,29 @@
1
  import openai
2
  import gradio as gr
3
- import random
4
 
5
- def generate_dialogues(api_key, generate_button):
6
- # Ensure the API key is set
7
- openai.api_key = api_key
8
 
9
- # Sample prompts provided
10
- sample_prompts = [
11
- "Don’t look now, but they’re right behind you.",
12
- "I need to talk to you about something.",
13
- "Oh no! You’re hurt!",
14
- "And that's why you're fired.",
15
- "What’s that on your arm?",
16
- "Is this seat taken?",
17
- "I can’t believe you actually got me a pony!"
18
- ]
19
-
20
- # Randomly select one of the provided prompts
21
- selected_prompt = random.choice(sample_prompts)
22
-
23
- # Build the prompt to guide the model towards generating a one-line dialogue
24
- prompt_text = (
25
- f"Based on the scenario: '{selected_prompt}', generate a one-line dialogue:\n"
26
- )
27
-
28
- # Generate dialogue based on the selected prompt
29
  response = openai.Completion.create(
30
  engine="davinci",
31
- prompt=prompt_text,
32
- max_tokens=18 # Adjusted to limit the response to a one-liner
33
  )
34
- dialogues = response['choices'][0]['text'].strip()
35
-
36
- # Return the generated dialogues
37
- return dialogues
38
-
39
- def run_interface():
40
- # Define Gradio interface
41
- iface = gr.Interface(
42
- fn=generate_dialogues,
43
- inputs=[
44
- gr.Textbox(label="OpenAI API Key"),
45
- gr.Button(label="Generate")
46
- ],
47
- outputs="text",
48
- live=False # Set to False so the function only runs when the button is pressed
49
  )
50
-
51
- # Launch the interface
52
- iface.launch()
53
 
54
  if __name__ == "__main__":
55
- run_interface()
 
1
  import openai
2
  import gradio as gr
 
3
 
4
+ # Set up the OpenAI API key
5
+ openai.api_key = 'YOUR_OPENAI_API_KEY'
 
6
 
7
+ def generate_statement():
8
+ # Generate a statement using OpenAI's GPT 3.5 model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  response = openai.Completion.create(
10
  engine="davinci",
11
+ prompt="Write a short one-line statement similar to common dialogue from movies or books:",
12
+ max_tokens=50
13
  )
14
+ return response.choices[0].text.strip()
15
+
16
+ def main():
17
+ # Define the UI components using gr.Interface
18
+ interface = gr.Interface(
19
+ fn=generate_statement, # Function to call on button press
20
+ inputs=[], # No input components
21
+ outputs="text", # Output is a text area
22
+ live=True # Generate statement without requiring a button press
 
 
 
 
 
 
23
  )
24
+
25
+ # Launch the UI on Spaces
26
+ interface.launch(share=True)
27
 
28
  if __name__ == "__main__":
29
+ main()