Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Agent, Task, Crew, Process
|
2 |
+
import gradio as gr
|
3 |
+
from langchain_groq import ChatGroq # Assuming this is your custom Groq integration
|
4 |
+
import os
|
5 |
+
import dotenv
|
6 |
+
|
7 |
+
# Load API keys from .env
|
8 |
+
dotenv.load_dotenv()
|
9 |
+
|
10 |
+
# Retrieve API key from .env file
|
11 |
+
api_key = os.getenv("Groq_API_KEY")
|
12 |
+
|
13 |
+
# Initialize Groq-powered LLaMA model using the API
|
14 |
+
llama_model = ChatGroq(
|
15 |
+
api_key=api_key,
|
16 |
+
model='llama3-groq-70b-8192-tool-use-preview' # Assuming this is the correct model name
|
17 |
+
)
|
18 |
+
|
19 |
+
# Define Plot Creator Agent
|
20 |
+
plot_creator_agent = Agent(
|
21 |
+
role="Plot Creator",
|
22 |
+
goal="""Create a compelling fictional plot based on the user's input about the genre, characters, and setting.""",
|
23 |
+
backstory="""You are a master storyteller, capable of weaving intricate plots for any genre. You take user inputs and craft the foundation of the story.""",
|
24 |
+
verbose=True,
|
25 |
+
allow_delegation=False,
|
26 |
+
llm=llama_model # Use the Groq LLaMA model for generating the plot
|
27 |
+
)
|
28 |
+
|
29 |
+
# Define Story Narrator Agent
|
30 |
+
story_narrator_agent = Agent(
|
31 |
+
role="Story Narrator",
|
32 |
+
goal="""Write a full fictional story based on the plot provided by the Plot Creator Agent.
|
33 |
+
The story should include vivid descriptions, engaging dialogue, and follow the narrative structure.""",
|
34 |
+
backstory="""You are a gifted writer who turns plot outlines into rich and immersive narratives, breathing life into the characters and setting.""",
|
35 |
+
verbose=True,
|
36 |
+
allow_delegation=False,
|
37 |
+
llm=llama_model # Use the Groq LLaMA model for generating the story
|
38 |
+
)
|
39 |
+
|
40 |
+
# Function to generate a fictional story based on user input
|
41 |
+
def generate_story(genre, characters, setting):
|
42 |
+
# Create tasks for the plot and story writing
|
43 |
+
plot_creation_task = Task(
|
44 |
+
description=f"Create a plot for a fictional story in the '{genre}' genre with the following characters: {characters}, "
|
45 |
+
f"and the story is set in: {setting}.",
|
46 |
+
agent=plot_creator_agent,
|
47 |
+
expected_output="A detailed plot with the main conflict, setting, and character arcs."
|
48 |
+
)
|
49 |
+
|
50 |
+
story_narration_task = Task(
|
51 |
+
description=f"Write a fictional story based on the generated plot. "
|
52 |
+
f"Include rich narrative elements such as character development, conflict resolution, and engaging dialogue.",
|
53 |
+
agent=story_narrator_agent,
|
54 |
+
expected_output="A 3-5 paragraph fictional story that fully immerses the reader in the characters and plot."
|
55 |
+
)
|
56 |
+
|
57 |
+
# Create a crew that orchestrates the agents and tasks
|
58 |
+
crew = Crew(
|
59 |
+
agents=[plot_creator_agent, story_narrator_agent],
|
60 |
+
tasks=[plot_creation_task, story_narration_task],
|
61 |
+
verbose=True,
|
62 |
+
process=Process.sequential # First generate plot, then write story
|
63 |
+
)
|
64 |
+
|
65 |
+
# Execute the tasks and return the final story output
|
66 |
+
output = crew.kickoff(inputs={'genre': genre, 'characters': characters, 'setting': setting})
|
67 |
+
return output.raw # Return the generated story
|
68 |
+
|
69 |
+
# Gradio Interface for Fictional Story Writer
|
70 |
+
iface = gr.Interface(
|
71 |
+
fn=generate_story, # Function to call when user submits input
|
72 |
+
inputs=[
|
73 |
+
gr.Textbox(label="Genre", placeholder="e.g., fantasy, sci-fi"),
|
74 |
+
gr.Textbox(label="Characters (comma-separated)", placeholder="e.g., knight, dragon, wizard"),
|
75 |
+
gr.Textbox(label="Setting", placeholder="e.g., a magical forest, outer space")
|
76 |
+
],
|
77 |
+
outputs="text", # Output is a text response (generated story)
|
78 |
+
title="Fictional Story Writer",
|
79 |
+
description="Generate a fictional story based on your input of genre, characters, and setting."
|
80 |
+
)
|
81 |
+
|
82 |
+
# Launch the Gradio app
|
83 |
+
iface.launch()
|