Spaces:
Running
Running
File size: 3,240 Bytes
26af61c fa9515b a8487ac 3692ffd 9da51e4 a8487ac 99534a2 26af61c fa9515b 26af61c fa9515b 26af61c fa9515b de94616 fa9515b 26af61c fa9515b 26af61c 14acb23 5444069 26af61c fa9515b 26af61c a8487ac 26af61c fa9515b 26af61c fa9515b 26af61c fa9515b 99534a2 fa9515b a8487ac fa9515b 99534a2 26af61c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import random
import os
import gradio as gr
import google.generativeai as genai
# Initialize the Gemini model
Model = genai.GenerativeModel('gemini-1.5-flash')
def chat_short_story(length, genre, theme, tone, writing_style):
"""
Generates a creative short story using Gemini API.
Parameters:
length (int): Approximate word count for the story.
genre (str): Genre of the story.
theme (str): Central theme of the story.
tone (str): Tone of the story.
writing_style (str): Writing style for the story.
Returns:
str: The generated short story, or an error message if unsuccessful.
"""
# Retrieve the Gemini API key from the environment
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GEMINI_API_KEY:
return "Error: Gemini API key not found. Please set the GEMINI_API_KEY environment variable."
# Configure the Gemini API
try:
genai.configure(api_key=GEMINI_API_KEY)
except Exception as e:
return f"Error: Failed to configure the Gemini API. Details: {e}"
# Construct the prompt
prompt = (
f"Write a creative short story of approximately {length} words in the {genre} genre. "
f"Use a {writing_style} writing style with a {tone} tone. "
f"The story should revolve around the theme of {theme}. "
f"Ensure it is engaging and includes a suitable title and always label the title as Title."
f"Make Sure to Never Repeat a Title and Story."
)
# Generate a response using the Gemini model
try:
chat = Model.start_chat()
response = chat.send_message(prompt)
return response.text
except Exception as e:
return f"Error: Failed to generate story. Details: {e}"
# Predefined options
Length = [100, 250, 750]
r_length = random.choice([l for l in Length if l >= 100])
Genre = [
"Fiction", "Nonfiction", "Drama", "Poetry", "Fantasy", "Horror", "Mystery",
"Science Fiction", "Suspense", "Women's fiction", "Supernatural/Paranormal", "Young adult"
]
r_genre = random.choice(Genre)
Themes = [
"Love", "Redemption", "Forgiveness", "Coming of age", "Revenge", "Good vs evil",
"Bravery and hardship", "The power of social status", "The destructive nature of love",
"The fallibility of the human condition"
]
r_themes = random.choice(Themes)
Writing_Styles = ["Expository", "Narrative", "Descriptive", "Persuasive", "Creative"]
r_Style = random.choice(Writing_Styles)
Tones = ["Formal", "Optimistic", "Worried", "Friendly", "Curious", "Assertive", "Encouraging"]
r_tones = random.choice(Tones)
# Gradio Interface setup
iface = gr.Interface(
fn=chat_short_story,
inputs=[
gr.Slider(value=100, label="Story Length", minimum=100, maximum=2500, step=50),
gr.Dropdown(label="Story Genre", choices=Genre),
gr.Dropdown(label="Story Theme", choices=Themes),
gr.Dropdown(label="Writing Style", choices=Writing_Styles),
gr.Dropdown(label="Story Tone", choices=Tones)
],
outputs=gr.Textbox(label="Generated Story"),
title="Welcome to Patrick's Story Generator",
description="Generate creative short stories tailored to your preferences."
)
iface.launch()
|