{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "d:\\Python\\Lib\\site-packages\\gradio\\blocks.py:890: UserWarning: api_name generate_description already exists, using generate_description_1\n", " warnings.warn(f\"api_name {api_name} already exists, using {api_name_}\")\n", "d:\\Python\\Lib\\site-packages\\gradio\\blocks.py:890: UserWarning: api_name generate_description already exists, using generate_description_2\n", " warnings.warn(f\"api_name {api_name} already exists, using {api_name_}\")\n", "d:\\Python\\Lib\\site-packages\\gradio\\blocks.py:890: UserWarning: api_name generate_description already exists, using generate_description_3\n", " warnings.warn(f\"api_name {api_name} already exists, using {api_name_}\")\n", "d:\\Python\\Lib\\site-packages\\gradio\\blocks.py:890: UserWarning: api_name generate_description already exists, using generate_description_4\n", " warnings.warn(f\"api_name {api_name} already exists, using {api_name_}\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7864\n", "Running on public URL: https://83de6007a68805d348.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import openai\n", "import gradio as gr\n", "\n", "openai.api_key = \"\" # Replace with your OpenAI API key\n", "\n", "def generate_description(*args):\n", " labels = [\n", " \"World Type\",\n", " \"Time Period\",\n", " \"Location\",\n", " \"Historical Context\",\n", " \"Cultural Atmosphere\",\n", " \"Major Conflict\",\n", " \"Plot Development\",\n", " \"Resolution\",\n", " \"Central Themes\",\n", " \"Tone and Style\",\n", " \"Character Identity\",\n", " \"Character Personal Experience\",\n", " \"Character Relationships\",\n", " \"Character Status\",\n", " \"Character Motivations\",\n", " \"Importance of Historical Accuracy\",\n", " \"Inspirations for the Story\"\n", " ]\n", "\n", " prompt_parts = []\n", " for label, value in zip(labels, args):\n", " if value: # Check if the input is provided\n", " prompt_parts.append(f\"{label}: {value}\")\n", "\n", " if not prompt_parts: # If no input is provided\n", " return \"Please provide at least one detail to generate a description.\"\n", "\n", " prompt = \"\\n\".join(prompt_parts) + \"\\n\\nGenerate a descriptive paragraph:\"\n", " response = openai.Completion.create(model='text-davinci-003', prompt=prompt, max_tokens=300)\n", " return response.choices[0].text if response.choices else \"Error in description generation.\"\n", "\n", "def iterative_refinement(initial_text):\n", " refined_text = initial_text\n", " for _ in range(3): # You can adjust the number of iterations\n", " prompt = refined_text + \"\\n\\nExpand and elaborate on this narrative:\"\n", " response = openai.Completion.create(\n", " model='text-davinci-003', \n", " prompt=prompt, \n", " max_tokens=500, \n", " temperature=0.8 \n", " )\n", " refined_text += \"\\n\\n\" + response.choices[0].text if response.choices else \"\"\n", " return refined_text\n", "\n", "def combine_and_refine_descriptions(*args):\n", " combined_text = \"\\n\\n\".join(args)\n", " return iterative_refinement(combined_text) if combined_text.strip() else \"No content to combine.\"\n", "def simple_string_matching(extracted, user_input):\n", " # Simple word-based matching, can be replaced with more complex algorithms\n", " matches = sum(1 for word in extracted.split() if word in user_input.split())\n", " total_words = len(user_input.split())\n", " return matches / total_words if total_words > 0 else 0\n", "\n", "def evaluate_fiction_accuracy(generated_fiction, world_type, time_period, location, historical_context, cultural_atmosphere):\n", " # Placeholder for user inputs as a list\n", " user_inputs = [world_type, time_period, location, historical_context, cultural_atmosphere]\n", "\n", " # Split generated fiction into words for simple matching\n", " fiction_words = set(generated_fiction.lower().split())\n", "\n", " # Calculate matches for each input\n", " matches = {label: int(any(word in fiction_words for word in input_text.lower().split())) \n", " for label, input_text in zip([\"World Type\", \"Time Period\", \"Location\", \"Historical Context\", \"Cultural Atmosphere\"], user_inputs)}\n", "\n", " # Calculate overall similarity score (basic version)\n", " total = len(matches)\n", " score = sum(matches.values()) / total if total > 0 else 0\n", "\n", " return score * 100, matches # Convert to percentage and return matches\n", " \n", "with gr.Blocks() as demo:\n", " with gr.Tab(\"Background\"):\n", " with gr.Row():\n", " with gr.Column(): \n", " world_type = gr.Textbox(label=\"What kind of world is it?\")\n", " time_period = gr.Textbox(label=\"Time Period\")\n", " location = gr.Textbox(label=\"Location\")\n", " historical_context = gr.Textbox(label=\"Historical Context\")\n", " cultural_atmosphere = gr.Textbox(label=\"Cultural Atmosphere\")\n", " background_btn = gr.Button(\"Generate Background Description\")\n", " with gr.Column():\n", " background_output = gr.Textbox(label=\"Background Description\", lines=18)\n", " background_btn.click(generate_description, inputs=[world_type, time_period, location, historical_context, cultural_atmosphere], outputs=background_output)\n", " \n", " with gr.Tab(\"Characters\"):\n", " with gr.Row():\n", " with gr.Column(): \n", " char_identity = gr.Textbox(label=\"Character Identity (male/female, job, ability)\")\n", " char_experience = gr.Textbox(label=\"Character Personal Experience, Special Character Design\")\n", " char_relationships = gr.Textbox(label=\"Character Relationships with Other Characters\")\n", " char_status = gr.Dropdown(label=\"Character Status in Story\", choices=[\"Main\", \"Secondary\", \"NPC\"])\n", " char_motivations = gr.Textbox(label=\"Character Motivations\")\n", " char_btn = gr.Button(\"Generate Character Description\")\n", " with gr.Column():\n", " char_output = gr.Textbox(label=\"Character Description\", lines=18)\n", " char_btn.click(generate_description, inputs=[char_identity, char_experience, char_relationships, char_status, char_motivations], outputs=char_output)\n", " \n", " with gr.Tab(\"Plot\"):\n", " with gr.Row():\n", " with gr.Column(): \n", " major_conflict = gr.Textbox(label=\"Major Conflict\")\n", " plot_development = gr.Textbox(label=\"Plot Development\")\n", " resolution = gr.Textbox(label=\"Resolution\")\n", " plot_btn = gr.Button(\"Generate Plot Description\")\n", " with gr.Column(): \n", " plot_output = gr.Textbox(label=\"Plot Description\", lines=18)\n", " plot_btn.click(generate_description, inputs=[major_conflict, plot_development, resolution], outputs=plot_output)\n", " \n", " \n", " with gr.Tab(\"Themes and Messages\"):\n", " with gr.Row():\n", " with gr.Column(): \n", " central_themes = gr.Textbox(label=\"Central Themes\")\n", " tone_style = gr.Textbox(label=\"Tone and Style\")\n", " themes_btn = gr.Button(\"Generate Themes Description\")\n", " with gr.Column(): \n", " themes_output = gr.Textbox(label=\"Themes Description\", lines=18)\n", " themes_btn.click(generate_description, inputs=[central_themes, tone_style], outputs=themes_output)\n", " \n", " with gr.Tab(\"Additional Details\"):\n", " with gr.Row():\n", " with gr.Column(): \n", " historical_accuracy = gr.Textbox(label=\"Importance of Historical Accuracy\")\n", " inspirations = gr.Textbox(label=\"Inspirations for the Story\")\n", " additional_btn = gr.Button(\"Generate Additional Details Description\")\n", " with gr.Column(): \n", " additional_output = gr.Textbox(label=\"Additional Details Description\", lines=18)\n", " additional_btn.click(generate_description, inputs=[historical_accuracy, inspirations], outputs=additional_output)\n", "\n", " with gr.Tab(\"Complete Fiction\"):\n", " refine_btn = gr.Button(\"Generate Refined Fiction\")\n", " refined_output = gr.Textbox(label=\"Refined Fiction\", lines=40)\n", "\n", " refine_btn.click(\n", " combine_and_refine_descriptions, \n", " inputs=[\n", " background_output, \n", " char_output, \n", " plot_output, \n", " themes_output, \n", " additional_output\n", " ], \n", " outputs=refined_output\n", " )\n", " with gr.Row():\n", " fiction_input = gr.Textbox(label=\"Generated Fiction\", lines=40)\n", " world_type_input = gr.Textbox(label=\"World Type\")\n", " time_period_input = gr.Textbox(label=\"Time Period\")\n", " location_input = gr.Textbox(label=\"Location\")\n", " historical_context_input = gr.Textbox(label=\"Historical Context\")\n", " cultural_atmosphere_input = gr.Textbox(label=\"Cultural Atmosphere\")\n", " evaluation_output = gr.Number(label=\"Similarity Score (%)\")\n", " matches_output = gr.Label(label=\"Matches Found\")\n", " evaluate_btn = gr.Button(\"Evaluate Fiction Accuracy\")\n", " evaluate_btn.click(\n", " evaluate_fiction_accuracy, \n", " inputs=[\n", " fiction_input, \n", " world_type_input, \n", " time_period_input, \n", " location_input, \n", " historical_context_input, \n", " cultural_atmosphere_input\n", " ], \n", " outputs=[evaluation_output, matches_output]\n", " )\n", "\n", "# Launch the Gradio interface\n", "demo.launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }