Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|