Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,191 +1,204 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import os
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import pytz
|
| 7 |
-
import re
|
| 8 |
-
import streamlit as st
|
| 9 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 10 |
from gradio_client import Client
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Cosmos DB configuration
|
| 18 |
-
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
| 19 |
-
Key = os.environ.get("Key")
|
| 20 |
-
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
| 21 |
-
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
anthropic_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
| 25 |
|
| 26 |
-
# Initialize
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
def
|
| 45 |
-
"""
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"query": query,
|
| 53 |
-
"response1": response1,
|
| 54 |
-
"response2": response2,
|
| 55 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 56 |
-
"type": "ai_response",
|
| 57 |
-
"version": "1.0"
|
| 58 |
-
}
|
| 59 |
-
container.create_item(body=record)
|
| 60 |
-
st.success(f"Record saved to Cosmos DB with ID: {record['id']}")
|
| 61 |
except Exception as e:
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
result_mixtral = client.predict(
|
| 71 |
-
query,
|
| 72 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 73 |
True,
|
| 74 |
api_name="/ask_llm"
|
| 75 |
)
|
| 76 |
-
|
| 77 |
-
# Get response from Mistral model
|
| 78 |
-
result_mistral = client.predict(
|
| 79 |
-
query,
|
| 80 |
-
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 81 |
-
True,
|
| 82 |
-
api_name="/ask_llm"
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
# Get RAG-enhanced response
|
| 86 |
-
result_rag = client.predict(
|
| 87 |
-
query,
|
| 88 |
-
10, # llm_results_use
|
| 89 |
-
"Semantic Search",
|
| 90 |
-
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 91 |
-
api_name="/update_with_rag_md"
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
return result_mixtral, result_mistral, result_rag
|
| 95 |
except Exception as e:
|
| 96 |
-
|
| 97 |
-
return None, None, None
|
| 98 |
-
|
| 99 |
-
def main():
|
| 100 |
-
st.title(title)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
st.subheader("Mistral Model Response")
|
| 131 |
-
st.markdown(result_mistral)
|
| 132 |
-
|
| 133 |
-
st.subheader("RAG-Enhanced Response")
|
| 134 |
-
if isinstance(result_rag, (list, tuple)) and len(result_rag) > 0:
|
| 135 |
-
st.markdown(result_rag[0])
|
| 136 |
-
if len(result_rag) > 1:
|
| 137 |
-
st.markdown(result_rag[1])
|
| 138 |
-
|
| 139 |
-
# Save results
|
| 140 |
-
filename = generate_filename(arxiv_query, "md")
|
| 141 |
-
create_file(filename, arxiv_query, f"{result_mixtral}\n\n{result_mistral}")
|
| 142 |
-
|
| 143 |
-
if container:
|
| 144 |
-
save_to_cosmos_db(container, arxiv_query, result_mixtral, result_mistral)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
if container:
|
| 176 |
-
save_to_cosmos_db(container, user_input, claude_response, "")
|
| 177 |
-
|
| 178 |
-
except Exception as e:
|
| 179 |
-
st.error(f"Error communicating with Claude: {str(e)}")
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
from moviepy.editor import ImageClip, concatenate_videoclips
|
| 8 |
from gradio_client import Client
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import edge_tts
|
| 11 |
+
import asyncio
|
| 12 |
+
import warnings
|
| 13 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
warnings.filterwarnings('ignore')
|
|
|
|
| 16 |
|
| 17 |
+
# Initialize the Gradio client for model access
|
| 18 |
+
client = Client("stabilityai/stable-diffusion-xl-base-1.0")
|
| 19 |
+
arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
| 20 |
|
| 21 |
+
STORY_GENRES = [
|
| 22 |
+
"Science Fiction",
|
| 23 |
+
"Fantasy",
|
| 24 |
+
"Mystery",
|
| 25 |
+
"Romance",
|
| 26 |
+
"Horror",
|
| 27 |
+
"Adventure",
|
| 28 |
+
"Historical Fiction",
|
| 29 |
+
"Comedy"
|
| 30 |
+
]
|
| 31 |
|
| 32 |
+
STORY_STRUCTURES = {
|
| 33 |
+
"Three Act": "Setup (Introduction, Inciting Incident) -> Confrontation (Rising Action, Climax) -> Resolution (Falling Action, Conclusion)",
|
| 34 |
+
"Hero's Journey": "Ordinary World -> Call to Adventure -> Trials -> Transformation -> Return",
|
| 35 |
+
"Five Act": "Exposition -> Rising Action -> Climax -> Falling Action -> Resolution",
|
| 36 |
+
"Seven Point": "Hook -> Plot Turn 1 -> Pinch Point 1 -> Midpoint -> Pinch Point 2 -> Plot Turn 2 -> Resolution"
|
| 37 |
+
}
|
| 38 |
|
| 39 |
+
async def generate_speech(text, voice="en-US-AriaNeural"):
|
| 40 |
+
"""Generate speech from text using edge-tts"""
|
| 41 |
try:
|
| 42 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 44 |
+
tmp_path = tmp_file.name
|
| 45 |
+
await communicate.save(tmp_path)
|
| 46 |
+
return tmp_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
+
print(f"Error in text2speech: {str(e)}")
|
| 49 |
+
raise
|
| 50 |
|
| 51 |
+
def generate_story_prompt(base_prompt, genre, structure):
|
| 52 |
+
"""Generate an expanded story prompt based on genre and structure"""
|
| 53 |
+
prompt = f"""Create a {genre} story using this concept: '{base_prompt}'
|
| 54 |
+
Follow this structure: {STORY_STRUCTURES[structure]}
|
| 55 |
+
Include vivid descriptions and sensory details.
|
| 56 |
+
Make it engaging and suitable for visualization.
|
| 57 |
+
Keep each scene description clear and detailed enough for image generation.
|
| 58 |
+
Limit the story to 5-7 key scenes.
|
| 59 |
+
"""
|
| 60 |
+
return prompt
|
| 61 |
+
|
| 62 |
+
def generate_story(prompt, model_choice):
|
| 63 |
+
"""Generate story using specified model"""
|
| 64 |
try:
|
| 65 |
+
result = arxiv_client.predict(
|
| 66 |
+
prompt,
|
| 67 |
+
model_choice,
|
|
|
|
|
|
|
|
|
|
| 68 |
True,
|
| 69 |
api_name="/ask_llm"
|
| 70 |
)
|
| 71 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
+
return f"Error generating story: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
def generate_image_from_text(text_prompt):
|
| 76 |
+
"""Generate an image from text description"""
|
| 77 |
+
try:
|
| 78 |
+
result = client.predict(
|
| 79 |
+
text_prompt,
|
| 80 |
+
num_inference_steps=30,
|
| 81 |
+
guidance_scale=7.5,
|
| 82 |
+
width=768,
|
| 83 |
+
height=512,
|
| 84 |
+
api_name="/text2image"
|
| 85 |
+
)
|
| 86 |
+
return result
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return None
|
| 89 |
|
| 90 |
+
def create_video_from_images(image_paths, durations):
|
| 91 |
+
"""Create video from a series of images"""
|
| 92 |
+
clips = [ImageClip(img_path).set_duration(dur) for img_path, dur in zip(image_paths, durations)]
|
| 93 |
+
final_clip = concatenate_videoclips(clips, method="compose")
|
| 94 |
+
output_path = tempfile.mktemp(suffix=".mp4")
|
| 95 |
+
final_clip.write_videofile(output_path, fps=24)
|
| 96 |
+
return output_path
|
| 97 |
|
| 98 |
+
def process_story(story_text, num_scenes=5):
|
| 99 |
+
"""Break story into scenes for visualization"""
|
| 100 |
+
sentences = story_text.split('.')
|
| 101 |
+
scenes = []
|
| 102 |
+
scene_length = max(1, len(sentences) // num_scenes)
|
| 103 |
+
|
| 104 |
+
for i in range(0, len(sentences), scene_length):
|
| 105 |
+
scene = '. '.join(sentences[i:i+scene_length]).strip()
|
| 106 |
+
if scene:
|
| 107 |
+
scenes.append(scene)
|
| 108 |
+
|
| 109 |
+
return scenes[:num_scenes]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
def story_generator_interface(prompt, genre, structure, model_choice, num_scenes, words_per_scene):
|
| 112 |
+
"""Main story generation and multimedia creation function"""
|
| 113 |
+
|
| 114 |
+
# Generate expanded prompt
|
| 115 |
+
story_prompt = generate_story_prompt(prompt, genre, structure)
|
| 116 |
+
|
| 117 |
+
# Generate story
|
| 118 |
+
story = generate_story(story_prompt, model_choice)
|
| 119 |
+
|
| 120 |
+
# Process story into scenes
|
| 121 |
+
scenes = process_story(story, num_scenes)
|
| 122 |
+
|
| 123 |
+
# Generate images for each scene
|
| 124 |
+
image_paths = []
|
| 125 |
+
for scene in scenes:
|
| 126 |
+
image = generate_image_from_text(scene)
|
| 127 |
+
if image is not None:
|
| 128 |
+
temp_path = tempfile.mktemp(suffix=".png")
|
| 129 |
+
Image.fromarray(image).save(temp_path)
|
| 130 |
+
image_paths.append(temp_path)
|
| 131 |
+
|
| 132 |
+
# Generate speech
|
| 133 |
+
audio_path = asyncio.run(generate_speech(story))
|
| 134 |
+
|
| 135 |
+
# Create video
|
| 136 |
+
scene_durations = [5.0] * len(image_paths) # 5 seconds per scene
|
| 137 |
+
video_path = create_video_from_images(image_paths, scene_durations)
|
| 138 |
+
|
| 139 |
+
return story, image_paths, audio_path, video_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Create Gradio interface
|
| 142 |
+
with gr.Blocks(title="AI Story Generator & Visualizer") as demo:
|
| 143 |
+
gr.Markdown("# ๐ญ AI Story Generator & Visualizer")
|
| 144 |
+
|
| 145 |
+
with gr.Row():
|
| 146 |
+
with gr.Column():
|
| 147 |
+
prompt_input = gr.Textbox(
|
| 148 |
+
label="Story Concept",
|
| 149 |
+
placeholder="Enter your story idea...",
|
| 150 |
+
lines=3
|
| 151 |
+
)
|
| 152 |
+
genre_input = gr.Dropdown(
|
| 153 |
+
label="Genre",
|
| 154 |
+
choices=STORY_GENRES,
|
| 155 |
+
value="Fantasy"
|
| 156 |
+
)
|
| 157 |
+
structure_input = gr.Dropdown(
|
| 158 |
+
label="Story Structure",
|
| 159 |
+
choices=list(STORY_STRUCTURES.keys()),
|
| 160 |
+
value="Three Act"
|
| 161 |
+
)
|
| 162 |
+
model_choice = gr.Dropdown(
|
| 163 |
+
label="Model",
|
| 164 |
+
choices=["mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.2"],
|
| 165 |
+
value="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 166 |
+
)
|
| 167 |
+
num_scenes = gr.Slider(
|
| 168 |
+
label="Number of Scenes",
|
| 169 |
+
minimum=3,
|
| 170 |
+
maximum=7,
|
| 171 |
+
value=5,
|
| 172 |
+
step=1
|
| 173 |
+
)
|
| 174 |
+
words_per_scene = gr.Slider(
|
| 175 |
+
label="Words per Scene",
|
| 176 |
+
minimum=20,
|
| 177 |
+
maximum=100,
|
| 178 |
+
value=50,
|
| 179 |
+
step=10
|
| 180 |
+
)
|
| 181 |
+
generate_btn = gr.Button("Generate Story & Media")
|
| 182 |
+
|
| 183 |
+
with gr.Row():
|
| 184 |
+
with gr.Column():
|
| 185 |
+
story_output = gr.Textbox(
|
| 186 |
+
label="Generated Story",
|
| 187 |
+
lines=10,
|
| 188 |
+
readonly=True
|
| 189 |
+
)
|
| 190 |
+
with gr.Column():
|
| 191 |
+
gallery = gr.Gallery(label="Scene Visualizations")
|
| 192 |
+
|
| 193 |
+
with gr.Row():
|
| 194 |
+
audio_output = gr.Audio(label="Story Narration")
|
| 195 |
+
video_output = gr.Video(label="Story Video")
|
| 196 |
+
|
| 197 |
+
generate_btn.click(
|
| 198 |
+
fn=story_generator_interface,
|
| 199 |
+
inputs=[prompt_input, genre_input, structure_input, model_choice, num_scenes, words_per_scene],
|
| 200 |
+
outputs=[story_output, gallery, audio_output, video_output]
|
| 201 |
+
)
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
+
demo.launch(reload=True)
|