Spaces:
Sleeping
Sleeping
import openai | |
import gradio as gr | |
openai.api_key = "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" # Replace with your OpenAI API key | |
def generate_description(*args): | |
labels = [ | |
"World Type", | |
"Time Period", | |
"Location", | |
"Historical Context", | |
"Cultural Atmosphere", | |
"Major Conflict", | |
"Plot Development", | |
"Resolution", | |
"Central Themes", | |
"Tone and Style", | |
"Character Identity", | |
"Character Personal Experience", | |
"Character Relationships", | |
"Character Status", | |
"Character Motivations", | |
"Importance of Historical Accuracy", | |
"Inspirations for the Story" | |
] | |
prompt_parts = [] | |
for label, value in zip(labels, args): | |
if value: # Check if the input is provided | |
prompt_parts.append(f"{label}: {value}") | |
if not prompt_parts: # If no input is provided | |
return "Please provide at least one detail to generate a description." | |
prompt = "\n".join(prompt_parts) + "\n\nGenerate a descriptive paragraph:" | |
response = openai.Completion.create(model='text-davinci-003', prompt=prompt, max_tokens=300) | |
return response.choices[0].text if response.choices else "Error in description generation." | |
def iterative_refinement(initial_text): | |
refined_text = initial_text | |
for _ in range(3): # You can adjust the number of iterations | |
prompt = refined_text + "\n\nExpand and elaborate on this narrative:" | |
response = openai.Completion.create( | |
model='text-davinci-003', | |
prompt=prompt, | |
max_tokens=500, | |
temperature=0.8 | |
) | |
refined_text += "\n\n" + response.choices[0].text if response.choices else "" | |
return refined_text | |
def combine_and_refine_descriptions(*args): | |
combined_text = "\n\n".join(args) | |
return iterative_refinement(combined_text) if combined_text.strip() else "No content to combine." | |
def simple_string_matching(extracted, user_input): | |
# Simple word-based matching, can be replaced with more complex algorithms | |
matches = sum(1 for word in extracted.split() if word in user_input.split()) | |
total_words = len(user_input.split()) | |
return matches / total_words if total_words > 0 else 0 | |
def evaluate_fiction_accuracy(generated_fiction, world_type, time_period, location, historical_context, | |
cultural_atmosphere): | |
# Placeholder for user inputs as a list | |
user_inputs = [world_type, time_period, location, historical_context, cultural_atmosphere] | |
# Split generated fiction into words for simple matching | |
fiction_words = set(generated_fiction.lower().split()) | |
# Calculate matches for each input | |
matches = {label: int(any(word in fiction_words for word in input_text.lower().split())) | |
for label, input_text in | |
zip(["World Type", "Time Period", "Location", "Historical Context", "Cultural Atmosphere"], user_inputs)} | |
# Calculate overall similarity score (basic version) | |
total = len(matches) | |
score = sum(matches.values()) / total if total > 0 else 0 | |
return score * 100, matches # Convert to percentage and return matches | |
with gr.Blocks() as demo: | |
with gr.Tab("Background"): | |
with gr.Row(): | |
with gr.Column(): | |
world_type = gr.Textbox(label="What kind of world is it?") | |
time_period = gr.Textbox(label="Time Period") | |
location = gr.Textbox(label="Location") | |
historical_context = gr.Textbox(label="Historical Context") | |
cultural_atmosphere = gr.Textbox(label="Cultural Atmosphere") | |
background_btn = gr.Button("Generate Background Description") | |
with gr.Column(): | |
background_output = gr.Textbox(label="Background Description", lines=18) | |
background_btn.click(generate_description, | |
inputs=[world_type, time_period, location, historical_context, | |
cultural_atmosphere], outputs=background_output) | |
with gr.Tab("Characters"): | |
with gr.Row(): | |
with gr.Column(): | |
char_identity = gr.Textbox(label="Character Identity (male/female, job, ability)") | |
char_experience = gr.Textbox(label="Character Personal Experience, Special Character Design") | |
char_relationships = gr.Textbox(label="Character Relationships with Other Characters") | |
char_status = gr.Dropdown(label="Character Status in Story", choices=["Main", "Secondary", "NPC"]) | |
char_motivations = gr.Textbox(label="Character Motivations") | |
char_btn = gr.Button("Generate Character Description") | |
with gr.Column(): | |
char_output = gr.Textbox(label="Character Description", lines=18) | |
char_btn.click(generate_description, | |
inputs=[char_identity, char_experience, char_relationships, char_status, | |
char_motivations], outputs=char_output) | |
with gr.Tab("Plot"): | |
with gr.Row(): | |
with gr.Column(): | |
major_conflict = gr.Textbox(label="Major Conflict") | |
plot_development = gr.Textbox(label="Plot Development") | |
resolution = gr.Textbox(label="Resolution") | |
plot_btn = gr.Button("Generate Plot Description") | |
with gr.Column(): | |
plot_output = gr.Textbox(label="Plot Description", lines=18) | |
plot_btn.click(generate_description, inputs=[major_conflict, plot_development, resolution], | |
outputs=plot_output) | |
with gr.Tab("Themes and Messages"): | |
with gr.Row(): | |
with gr.Column(): | |
central_themes = gr.Textbox(label="Central Themes") | |
tone_style = gr.Textbox(label="Tone and Style") | |
themes_btn = gr.Button("Generate Themes Description") | |
with gr.Column(): | |
themes_output = gr.Textbox(label="Themes Description", lines=18) | |
themes_btn.click(generate_description, inputs=[central_themes, tone_style], outputs=themes_output) | |
with gr.Tab("Additional Details"): | |
with gr.Row(): | |
with gr.Column(): | |
historical_accuracy = gr.Textbox(label="Importance of Historical Accuracy") | |
inspirations = gr.Textbox(label="Inspirations for the Story") | |
additional_btn = gr.Button("Generate Additional Details Description") | |
with gr.Column(): | |
additional_output = gr.Textbox(label="Additional Details Description", lines=18) | |
additional_btn.click(generate_description, inputs=[historical_accuracy, inspirations], | |
outputs=additional_output) | |
with gr.Tab("Complete Fiction"): | |
refine_btn = gr.Button("Generate Refined Fiction") | |
refined_output = gr.Textbox(label="Refined Fiction", lines=40) | |
refine_btn.click( | |
combine_and_refine_descriptions, | |
inputs=[ | |
background_output, | |
char_output, | |
plot_output, | |
themes_output, | |
additional_output | |
], | |
outputs=refined_output | |
) | |
with gr.Row(): | |
fiction_input = gr.Textbox(label="Generated Fiction", lines=40) | |
world_type_input = gr.Textbox(label="World Type") | |
time_period_input = gr.Textbox(label="Time Period") | |
location_input = gr.Textbox(label="Location") | |
historical_context_input = gr.Textbox(label="Historical Context") | |
cultural_atmosphere_input = gr.Textbox(label="Cultural Atmosphere") | |
evaluation_output = gr.Number(label="Similarity Score (%)") | |
matches_output = gr.Label(label="Matches Found") | |
evaluate_btn = gr.Button("Evaluate Fiction Accuracy") | |
evaluate_btn.click( | |
evaluate_fiction_accuracy, | |
inputs=[ | |
fiction_input, | |
world_type_input, | |
time_period_input, | |
location_input, | |
historical_context_input, | |
cultural_atmosphere_input | |
], | |
outputs=[evaluation_output, matches_output] | |
) | |
# Launch the Gradio interface | |
demo.launch(share=True) |