Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
from langchain_tavily import TavilySearch
|
6 |
+
from langgraph.prebuilt import create_react_agent
|
7 |
+
from langgraph.checkpoint.memory import InMemorySaver
|
8 |
+
import uuid
|
9 |
+
|
10 |
+
# Set up LLM
|
11 |
+
llm = ChatGroq(
|
12 |
+
temperature=0,
|
13 |
+
groq_api_key='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',
|
14 |
+
model_name="llama-3.3-70b-versatile"
|
15 |
+
)
|
16 |
+
|
17 |
+
# Define tools
|
18 |
+
search_tool = TavilySearch(
|
19 |
+
max_results=5,
|
20 |
+
tavily_api_key='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'
|
21 |
+
)
|
22 |
+
|
23 |
+
tools = [search_tool]
|
24 |
+
|
25 |
+
# Define system prompt
|
26 |
+
system_prompt = """
|
27 |
+
You are a professional content strategist and scriptwriter for Instagram Reels.
|
28 |
+
Your job is to help content creators generate high-performing short-form video scripts (Reels) based on a theme or rough idea they provide.
|
29 |
+
|
30 |
+
--------------------------------------
|
31 |
+
|
32 |
+
INPUT FROM USER:
|
33 |
+
Theme or Idea: {text}
|
34 |
+
|
35 |
+
--------------------------------------
|
36 |
+
|
37 |
+
Instructions:
|
38 |
+
1. Understand the topic and tone from the input. If the input is a vague idea, clarify the theme internally before writing.
|
39 |
+
2. The target audience is Instagram users (18–35), who enjoy short, engaging, and value-packed content.
|
40 |
+
3. Keep the script short, clear, and suitable for a 30–60 second video.
|
41 |
+
4. The tone should be friendly, motivational, and relatable (unless specified otherwise).
|
42 |
+
5. Use emojis in the caption and keep hashtags relevant to the theme.
|
43 |
+
|
44 |
+
--------------------------------------
|
45 |
+
|
46 |
+
OUTPUT FORMAT:
|
47 |
+
- Hook: A scroll-stopping first line (1 sentence only).
|
48 |
+
- 3-Point Structure or Mini Story:
|
49 |
+
1. [Point or part 1]
|
50 |
+
2. [Point or part 2]
|
51 |
+
3. [Point or part 3]
|
52 |
+
- Caption: A 1–2 sentence engaging caption.
|
53 |
+
- Hashtags: Include 3–5 relevant hashtags.
|
54 |
+
- Call-to-Action: End with a line like “Follow for more tips”, “Save this”, etc.
|
55 |
+
|
56 |
+
Only respond with the script in this format. Do not explain your reasoning.
|
57 |
+
|
58 |
+
--------------------------------------
|
59 |
+
"""
|
60 |
+
|
61 |
+
# Create checkpoint
|
62 |
+
checkpointer = InMemorySaver()
|
63 |
+
|
64 |
+
# Create LangGraph agent
|
65 |
+
agent = create_react_agent(
|
66 |
+
model=llm,
|
67 |
+
tools=tools,
|
68 |
+
prompt=system_prompt,
|
69 |
+
checkpointer=checkpointer
|
70 |
+
)
|
71 |
+
|
72 |
+
# Script generation function for Gradio
|
73 |
+
def generate_script(text: str):
|
74 |
+
try:
|
75 |
+
thread_id = str(uuid.uuid4()) # Generate a unique thread ID
|
76 |
+
config = {"configurable": {"thread_id": thread_id}}
|
77 |
+
response = agent.invoke(
|
78 |
+
{"messages": [{"role": "user", "content": text}]},
|
79 |
+
config
|
80 |
+
)
|
81 |
+
return response['messages'][-1].content
|
82 |
+
except Exception as e:
|
83 |
+
return f"Error: {str(e)}"
|
84 |
+
|
85 |
+
# Gradio UI
|
86 |
+
interface = gr.Interface(
|
87 |
+
fn=generate_script,
|
88 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your Reel idea or theme here..."),
|
89 |
+
outputs=gr.Textbox(label="Generated Reel Script"),
|
90 |
+
title="Instagram Reel Script Generator 🎬",
|
91 |
+
description="Enter your idea or theme and get a full short-form video script tailored for Instagram Reels."
|
92 |
+
)
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
interface.launch(share=True)
|