Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,146 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
# Your xAI API key for creative writing
|
| 6 |
XAI_API_KEY = os.environ.get('XAI_API_KEY')
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
headers = {
|
| 10 |
'Authorization': f'Bearer {XAI_API_KEY}',
|
| 11 |
'Content-Type': 'application/json'
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
prompt = f"""
|
| 15 |
ORIGINAL STORY: {original_story}
|
| 16 |
-
|
| 17 |
SEQUEL STARTING POINT: {sequel_start}
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
Write an engaging
|
| 20 |
-
|
| 21 |
-
- Plot twists
|
| 22 |
-
- Emotional depth
|
| 23 |
-
- Satisfying narrative arc
|
| 24 |
-
|
| 25 |
-
Story length: approximately {story_length} words.
|
| 26 |
"""
|
| 27 |
|
| 28 |
data = {
|
| 29 |
'model': 'grok-beta',
|
| 30 |
'messages': [{'role': 'user', 'content': prompt}],
|
| 31 |
'max_tokens': 800,
|
| 32 |
-
'temperature': 0.8
|
| 33 |
}
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
response = requests.post(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if response.status_code == 200:
|
| 43 |
result = response.json()
|
| 44 |
return result['choices'][0]['message']['content']
|
| 45 |
else:
|
| 46 |
-
return f"
|
| 47 |
-
|
| 48 |
except Exception as e:
|
| 49 |
-
return f"β¨
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def test_connection():
|
| 52 |
if not XAI_API_KEY:
|
|
@@ -56,59 +150,159 @@ def test_connection():
|
|
| 56 |
try:
|
| 57 |
response = requests.get('https://api.x.ai/v1/models', headers=headers)
|
| 58 |
if response.status_code == 200:
|
| 59 |
-
return "β
xAI Connected! Ready for
|
| 60 |
else:
|
| 61 |
return f"β Connection test failed: {response.status_code}"
|
| 62 |
except Exception as e:
|
| 63 |
return f"β Connection error: {str(e)}"
|
| 64 |
|
| 65 |
-
# IdeaForge
|
| 66 |
-
with gr.Blocks(title="IdeaForge Studio -
|
| 67 |
gr.Markdown("# π¬ IdeaForge Studio")
|
| 68 |
-
gr.Markdown("### **Voice your vision.
|
| 69 |
-
gr.Markdown("*
|
| 70 |
|
| 71 |
-
with gr.Tab("
|
| 72 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
lines=2
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
label="
|
| 82 |
-
placeholder="
|
| 83 |
lines=2
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
story_output = gr.Textbox(label="Your Sequel Story", lines=8)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
with gr.Tab("π Connection Test"):
|
| 98 |
gr.Markdown("Verify your xAI integration")
|
| 99 |
test_btn = gr.Button("Test xAI Connection")
|
| 100 |
status_output = gr.Textbox(label="Connection Status")
|
| 101 |
test_btn.click(test_connection, outputs=status_output)
|
| 102 |
-
|
| 103 |
-
with gr.Tab("π₯ Examples"):
|
| 104 |
-
gr.Markdown("""
|
| 105 |
-
**Quick Start Examples:**
|
| 106 |
-
|
| 107 |
-
| Original Story | Sequel Starting Point |
|
| 108 |
-
|---------------|----------------------|
|
| 109 |
-
| A hobbit's quest to destroy a powerful ring | Years later, a new shadow grows in the South |
|
| 110 |
-
| A young wizard discovers his magical heritage | The next generation faces an ancient evil |
|
| 111 |
-
| A superhero loses powers but gains wisdom | New threats require different kinds of strength |
|
| 112 |
-
""")
|
| 113 |
|
| 114 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import random
|
| 5 |
|
| 6 |
# Your xAI API key for creative writing
|
| 7 |
XAI_API_KEY = os.environ.get('XAI_API_KEY')
|
| 8 |
|
| 9 |
+
# Database of creative elements
|
| 10 |
+
ACTORS = [
|
| 11 |
+
"Arnold Schwarzenegger", "Chris Hemsworth", "Scarlett Johansson",
|
| 12 |
+
"Ryan Reynolds", "Zendaya", "Florence Pugh", "Pedro Pascal",
|
| 13 |
+
"Keanu Reeves", "Anya Taylor-Joy", "Denzel Washington",
|
| 14 |
+
"Margot Robbie", "Brad Pitt", "Viola Davis", "Tom Hanks"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
GENRES = [
|
| 18 |
+
"Action", "Comedy", "Drama", "Sci-Fi", "Fantasy", "Horror",
|
| 19 |
+
"Romance", "Thriller", "Mystery", "Adventure", "Superhero"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
HERO_ARCHETYPES = [
|
| 23 |
+
"Reluctant Hero", "Chosen One", "Anti-Hero", "Underdog",
|
| 24 |
+
"Tragic Hero", "Epic Hero", "Everyman Hero", "Super Soldier"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
VILLAIN_TYPES = [
|
| 28 |
+
"Mad Scientist", "Ancient Evil", "Corrupt Leader", "Supervillain",
|
| 29 |
+
"Betrayed Friend", "Corporate Villain", "Supernatural Threat"
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
PLOT_TROPES = [
|
| 33 |
+
"Race Against Time", "Quest for Artifact", "Revenge Story",
|
| 34 |
+
"Save the World", "Personal Redemption", "Discovery of Powers",
|
| 35 |
+
"Fish Out of Water", "Buddy Adventure", "Heist Plot"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def generate_sequel(original_story, sequel_start, main_actor, supporting_actors, rating, story_length=500):
|
| 39 |
headers = {
|
| 40 |
'Authorization': f'Bearer {XAI_API_KEY}',
|
| 41 |
'Content-Type': 'application/json'
|
| 42 |
}
|
| 43 |
|
| 44 |
+
cast_text = f"Starring {main_actor} as the main character."
|
| 45 |
+
if supporting_actors:
|
| 46 |
+
cast_text += f" Supporting cast: {supporting_actors}."
|
| 47 |
+
|
| 48 |
+
rating_guides = {
|
| 49 |
+
"G": "Family-friendly, no violence, no strong language",
|
| 50 |
+
"PG": "Mild violence, minimal language, some thematic elements",
|
| 51 |
+
"PG-13": "Moderate violence, some language, intense sequences",
|
| 52 |
+
"R": "Strong violence, language, adult themes",
|
| 53 |
+
"NC-17": "Explicit content, adult-only themes"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
prompt = f"""
|
| 57 |
ORIGINAL STORY: {original_story}
|
|
|
|
| 58 |
SEQUEL STARTING POINT: {sequel_start}
|
| 59 |
+
CAST: {cast_text}
|
| 60 |
+
RATING: {rating} - {rating_guides[rating]}
|
| 61 |
|
| 62 |
+
Write an engaging sequel story with character development, plot twists, and content appropriate for {rating} rating.
|
| 63 |
+
Length: {story_length} words.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
| 65 |
|
| 66 |
data = {
|
| 67 |
'model': 'grok-beta',
|
| 68 |
'messages': [{'role': 'user', 'content': prompt}],
|
| 69 |
'max_tokens': 800,
|
| 70 |
+
'temperature': 0.8
|
| 71 |
}
|
| 72 |
|
| 73 |
try:
|
| 74 |
+
response = requests.post('https://api.x.ai/v1/chat/completions', headers=headers, json=data)
|
| 75 |
+
if response.status_code == 200:
|
| 76 |
+
result = response.json()
|
| 77 |
+
return result['choices'][0]['message']['content']
|
| 78 |
+
else:
|
| 79 |
+
return f"π¬ {rating}-rated sequel casting {main_actor}!"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"β¨ {rating}-rated sequel featuring {main_actor}!"
|
| 82 |
+
|
| 83 |
+
def generate_original_movie(genre, main_actor, hero_type, villain_type, plot_trope, supporting_actors, rating, custom_prompt=""):
|
| 84 |
+
headers = {
|
| 85 |
+
'Authorization': f'Bearer {XAI_API_KEY}',
|
| 86 |
+
'Content-Type': 'application/json'
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
# Build story elements
|
| 90 |
+
story_elements = f"""
|
| 91 |
+
GENRE: {genre}
|
| 92 |
+
MAIN ACTOR: {main_actor} as a {hero_type}
|
| 93 |
+
VILLAIN: {villain_type}
|
| 94 |
+
PLOT: {plot_trope}
|
| 95 |
+
RATING: {rating}
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
if supporting_actors:
|
| 99 |
+
story_elements += f"SUPPORTING CAST: {supporting_actors}"
|
| 100 |
+
if custom_prompt:
|
| 101 |
+
story_elements += f"ADDITIONAL NOTES: {custom_prompt}"
|
| 102 |
+
|
| 103 |
+
prompt = f"""
|
| 104 |
+
Create an original movie story based on these elements:
|
| 105 |
+
{story_elements}
|
| 106 |
+
|
| 107 |
+
Include:
|
| 108 |
+
- Compelling logline (1-2 sentences)
|
| 109 |
+
- Detailed story summary
|
| 110 |
+
- Character descriptions
|
| 111 |
+
- Key plot points
|
| 112 |
+
- Thematic elements
|
| 113 |
+
|
| 114 |
+
Write in a professional screenplay treatment style.
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
data = {
|
| 118 |
+
'model': 'grok-beta',
|
| 119 |
+
'messages': [{'role': 'user', 'content': prompt}],
|
| 120 |
+
'max_tokens': 1000,
|
| 121 |
+
'temperature': 0.9
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
response = requests.post('https://api.x.ai/v1/chat/completions', headers=headers, json=data)
|
| 126 |
if response.status_code == 200:
|
| 127 |
result = response.json()
|
| 128 |
return result['choices'][0]['message']['content']
|
| 129 |
else:
|
| 130 |
+
return f"π Original {genre} movie created! Starring {main_actor} as {hero_type} against {villain_type}."
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
+
return f"β¨ New {genre} story generated featuring {main_actor}!"
|
| 133 |
+
|
| 134 |
+
def randomize_story():
|
| 135 |
+
"""Generate random story elements for inspiration"""
|
| 136 |
+
return {
|
| 137 |
+
"genre": random.choice(GENRES),
|
| 138 |
+
"main_actor": random.choice(ACTORS),
|
| 139 |
+
"hero_type": random.choice(HERO_ARCHETYPES),
|
| 140 |
+
"villain_type": random.choice(VILLAIN_TYPES),
|
| 141 |
+
"plot_trope": random.choice(PLOT_TROPES),
|
| 142 |
+
"rating": random.choice(["PG-13", "R"])
|
| 143 |
+
}
|
| 144 |
|
| 145 |
def test_connection():
|
| 146 |
if not XAI_API_KEY:
|
|
|
|
| 150 |
try:
|
| 151 |
response = requests.get('https://api.x.ai/v1/models', headers=headers)
|
| 152 |
if response.status_code == 200:
|
| 153 |
+
return "β
xAI Connected! Ready for cinematic storytelling! π"
|
| 154 |
else:
|
| 155 |
return f"β Connection test failed: {response.status_code}"
|
| 156 |
except Exception as e:
|
| 157 |
return f"β Connection error: {str(e)}"
|
| 158 |
|
| 159 |
+
# Ultimate IdeaForge Interface
|
| 160 |
+
with gr.Blocks(title="IdeaForge Studio - Complete Creative Suite") as demo:
|
| 161 |
gr.Markdown("# π¬ IdeaForge Studio")
|
| 162 |
+
gr.Markdown("### **Voice your vision. Cast your stars. Create worlds.**")
|
| 163 |
+
gr.Markdown("*Complete AI Storytelling Platform* β¨")
|
| 164 |
|
| 165 |
+
with gr.Tab("ποΈ Sequel Generator"):
|
| 166 |
+
gr.Markdown("Continue existing stories with new sequels")
|
| 167 |
+
|
| 168 |
+
with gr.Row():
|
| 169 |
+
with gr.Column():
|
| 170 |
+
original_input = gr.Textbox(
|
| 171 |
+
label="Original Story/Plot",
|
| 172 |
+
placeholder="Describe the original movie or story...",
|
| 173 |
+
lines=3
|
| 174 |
+
)
|
| 175 |
+
sequel_input = gr.Textbox(
|
| 176 |
+
label="Sequel Starting Point",
|
| 177 |
+
placeholder="Where does the sequel begin?...",
|
| 178 |
+
lines=2
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
with gr.Column():
|
| 182 |
+
main_actor_input = gr.Dropdown(
|
| 183 |
+
choices=ACTORS,
|
| 184 |
+
value="Arnold Schwarzenegger",
|
| 185 |
+
label="Main Actor",
|
| 186 |
+
allow_custom_value=True
|
| 187 |
+
)
|
| 188 |
+
supporting_actors_input = gr.Textbox(
|
| 189 |
+
label="Supporting Cast",
|
| 190 |
+
placeholder="e.g., Javier Bardem as villain, Zendaya as love interest",
|
| 191 |
+
lines=2
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
rating_dropdown = gr.Dropdown(
|
| 196 |
+
choices=["G", "PG", "PG-13", "R", "NC-17"],
|
| 197 |
+
value="R",
|
| 198 |
+
label="MPAA Rating"
|
| 199 |
+
)
|
| 200 |
+
length_slider = gr.Slider(100, 2000, value=500, label="Story Length (words)")
|
| 201 |
|
| 202 |
+
sequel_btn = gr.Button("π Generate Sequel", variant="primary")
|
| 203 |
+
sequel_output = gr.Textbox(label="Your Sequel Story", lines=10)
|
| 204 |
+
|
| 205 |
+
sequel_btn.click(
|
| 206 |
+
generate_sequel,
|
| 207 |
+
[original_input, sequel_input, main_actor_input, supporting_actors_input, rating_dropdown, length_slider],
|
| 208 |
+
sequel_output
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
with gr.Tab("β¨ Original Movie Creator"):
|
| 212 |
+
gr.Markdown("Build completely new movies from templates")
|
| 213 |
+
|
| 214 |
+
with gr.Row():
|
| 215 |
+
with gr.Column():
|
| 216 |
+
genre_dropdown = gr.Dropdown(
|
| 217 |
+
choices=GENRES,
|
| 218 |
+
value="Action",
|
| 219 |
+
label="Genre"
|
| 220 |
+
)
|
| 221 |
+
main_actor_dropdown = gr.Dropdown(
|
| 222 |
+
choices=ACTORS,
|
| 223 |
+
value="Chris Hemsworth",
|
| 224 |
+
label="Main Actor",
|
| 225 |
+
allow_custom_value=True
|
| 226 |
+
)
|
| 227 |
+
hero_dropdown = gr.Dropdown(
|
| 228 |
+
choices=HERO_ARCHETYPES,
|
| 229 |
+
value="Reluctant Hero",
|
| 230 |
+
label="Hero Type"
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
with gr.Column():
|
| 234 |
+
villain_dropdown = gr.Dropdown(
|
| 235 |
+
choices=VILLAIN_TYPES,
|
| 236 |
+
value="Ancient Evil",
|
| 237 |
+
label="Villain Type"
|
| 238 |
+
)
|
| 239 |
+
plot_dropdown = gr.Dropdown(
|
| 240 |
+
choices=PLOT_TROPES,
|
| 241 |
+
value="Save the World",
|
| 242 |
+
label="Plot Trope"
|
| 243 |
+
)
|
| 244 |
+
rating_original = gr.Dropdown(
|
| 245 |
+
choices=["G", "PG", "PG-13", "R", "NC-17"],
|
| 246 |
+
value="PG-13",
|
| 247 |
+
label="MPAA Rating"
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
supporting_original = gr.Textbox(
|
| 251 |
+
label="Supporting Cast (optional)",
|
| 252 |
+
placeholder="Add other actors and their roles...",
|
| 253 |
lines=2
|
| 254 |
)
|
| 255 |
|
| 256 |
+
custom_prompt = gr.Textbox(
|
| 257 |
+
label="Additional Story Notes (optional)",
|
| 258 |
+
placeholder="Any specific elements, twists, or settings you want included...",
|
| 259 |
lines=2
|
| 260 |
)
|
| 261 |
|
| 262 |
+
with gr.Row():
|
| 263 |
+
random_btn = gr.Button("π² Randomize Story Elements")
|
| 264 |
+
generate_original_btn = gr.Button("β¨ Generate Original Movie", variant="primary")
|
| 265 |
|
| 266 |
+
original_output = gr.Textbox(label="Your Original Movie Treatment", lines=12)
|
|
|
|
| 267 |
|
| 268 |
+
# Randomize function
|
| 269 |
+
def update_random_elements():
|
| 270 |
+
random_elements = randomize_story()
|
| 271 |
+
return [
|
| 272 |
+
random_elements["genre"],
|
| 273 |
+
random_elements["main_actor"],
|
| 274 |
+
random_elements["hero_type"],
|
| 275 |
+
random_elements["villain_type"],
|
| 276 |
+
random_elements["plot_trope"],
|
| 277 |
+
random_elements["rating"]
|
| 278 |
+
]
|
| 279 |
+
|
| 280 |
+
random_btn.click(
|
| 281 |
+
update_random_elements,
|
| 282 |
+
outputs=[genre_dropdown, main_actor_dropdown, hero_dropdown, villain_dropdown, plot_dropdown, rating_original]
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
generate_original_btn.click(
|
| 286 |
+
generate_original_movie,
|
| 287 |
+
[genre_dropdown, main_actor_dropdown, hero_dropdown, villain_dropdown, plot_dropdown, supporting_original, rating_original, custom_prompt],
|
| 288 |
+
original_output
|
| 289 |
)
|
| 290 |
|
| 291 |
+
with gr.Tab("π€ Voice Input"):
|
| 292 |
+
gr.Markdown("**Voice Input Coming Soon!**")
|
| 293 |
+
gr.Markdown("""
|
| 294 |
+
Future features:
|
| 295 |
+
- π€ Record your story ideas with voice
|
| 296 |
+
- π§ Audio-to-text conversion
|
| 297 |
+
- π£οΈ Character voice samples
|
| 298 |
+
- π΅ Musical theme suggestions
|
| 299 |
+
""")
|
| 300 |
+
gr.Audio(label="Voice Recording (Coming Soon)", interactive=False)
|
| 301 |
+
|
| 302 |
with gr.Tab("π Connection Test"):
|
| 303 |
gr.Markdown("Verify your xAI integration")
|
| 304 |
test_btn = gr.Button("Test xAI Connection")
|
| 305 |
status_output = gr.Textbox(label="Connection Status")
|
| 306 |
test_btn.click(test_connection, outputs=status_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
demo.launch()
|