File size: 7,158 Bytes
1e91094
 
 
96d7e87
 
 
 
 
1e91094
 
 
33d5150
 
 
1e91094
96d7e87
1e91094
33d5150
2db8e33
 
 
 
 
 
 
33d5150
96d7e87
1e91094
96d7e87
 
1e91094
 
 
 
 
33d5150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8e33
 
 
 
 
 
 
33d5150
2db8e33
33d5150
 
 
 
1e91094
 
 
96d7e87
1e91094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33d5150
1e91094
 
 
 
33d5150
 
1e91094
 
 
33d5150
 
 
 
1e91094
33d5150
1e91094
2db8e33
 
 
 
 
 
 
 
 
 
 
 
96d7e87
 
 
 
33d5150
1e91094
 
96d7e87
33d5150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2db8e33
33d5150
1e91094
33d5150
 
 
 
2db8e33
33d5150
96d7e87
 
33d5150
 
2db8e33
96d7e87
33d5150
96d7e87
 
 
 
33d5150
96d7e87
2db8e33
 
 
 
 
 
1e91094
 
96d7e87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import gradio as gr
import random
from datetime import datetime
import tempfile
import os
import edge_tts
import asyncio
import warnings
import pytz
import re
import json
import pandas as pd
from pathlib import Path
from gradio_client import Client

warnings.filterwarnings('ignore')

# Initialize story starters
STORY_STARTERS = [
    ['Adventure', 'In a hidden temple deep in the Amazon...'],
    ['Mystery', 'The detective found an unusual note...'],
    ['Romance', 'Two strangers meet on a rainy evening...'],
    ['Sci-Fi', 'The space station received an unexpected signal...'],
    ['Fantasy', 'A magical portal appeared in the garden...']
]

# Initialize client outside of interface definition
arxiv_client = None

def init_client():
    global arxiv_client
    if arxiv_client is None:
        arxiv_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
    return arxiv_client

def save_story(story, audio_path):
    """Save story and audio to gallery"""
    try:
        # Create gallery directory if it doesn't exist
        gallery_dir = Path("gallery")
        gallery_dir.mkdir(exist_ok=True)
        
        # Generate timestamp for unique filename
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        
        # Save story text
        story_path = gallery_dir / f"story_{timestamp}.txt"
        with open(story_path, "w") as f:
            f.write(story)
        
        # Copy audio file to gallery
        if audio_path:
            new_audio_path = gallery_dir / f"audio_{timestamp}.mp3"
            os.system(f"cp {audio_path} {str(new_audio_path)}")
            
        return str(story_path), str(new_audio_path)
    except Exception as e:
        print(f"Error saving to gallery: {str(e)}")
        return None, None

def load_gallery():
    """Load all stories and audio from gallery"""
    try:
        gallery_dir = Path("gallery")
        if not gallery_dir.exists():
            return []
        
        files = []
        for story_file in gallery_dir.glob("story_*.txt"):
            timestamp = story_file.stem.split('_')[1]
            audio_file = gallery_dir / f"audio_{timestamp}.mp3"
            
            with open(story_file) as f:
                story_text = f.read()
            
            # Format as list instead of dict for Gradio Dataframe
            files.append([
                timestamp,
                story_text[:100] + "...",
                str(story_file),
                str(audio_file) if audio_file.exists() else None
            ])
        
        return sorted(files, key=lambda x: x[0], reverse=True)
    except Exception as e:
        print(f"Error loading gallery: {str(e)}")
        return []

def generate_story(prompt, model_choice):
    """Generate story using specified model"""
    try:
        client = init_client()
        if client is None:
            return "Error: Story generation service is not available."
        
        result = client.predict(
            prompt=prompt,
            llm_model_picked=model_choice,
            stream_outputs=True,
            api_name="/ask_llm"
        )
        return result
    except Exception as e:
        return f"Error generating story: {str(e)}"

async def generate_speech(text, voice="en-US-AriaNeural"):
    """Generate speech from text"""
    try:
        communicate = edge_tts.Communicate(text, voice)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
            tmp_path = tmp_file.name
            await communicate.save(tmp_path)
        return tmp_path
    except Exception as e:
        print(f"Error in text2speech: {str(e)}")
        return None

def process_story_and_audio(prompt, model_choice):
    """Process story, generate audio, and save to gallery"""
    try:
        # Generate story
        story = generate_story(prompt, model_choice)
        if isinstance(story, str) and story.startswith("Error"):
            return story, None, None
        
        # Generate audio
        audio_path = asyncio.run(generate_speech(story))
        
        # Save to gallery
        story_path, saved_audio_path = save_story(story, audio_path)
        
        return story, audio_path, load_gallery()
    except Exception as e:
        return f"Error: {str(e)}", None, None

def play_gallery_audio(evt: gr.SelectData, gallery_data):
    """Play audio from gallery selection"""
    try:
        selected_row = gallery_data[evt.index[0]]
        audio_path = selected_row[3]  # Audio path is the fourth element
        if audio_path and os.path.exists(audio_path):
            return audio_path
        return None
    except Exception as e:
        print(f"Error playing gallery audio: {str(e)}")
        return None

# Create the Gradio interface
with gr.Blocks(title="AI Story Generator") as demo:
    gr.Markdown("""
    # 🎭 AI Story Generator & Narrator
    Generate creative stories, listen to them, and build your gallery!
    """)
    
    with gr.Row():
        with gr.Column(scale=3):
            with gr.Row():
                prompt_input = gr.Textbox(
                    label="Story Concept",
                    placeholder="Enter your story idea...",
                    lines=3
                )
            
            with gr.Row():
                model_choice = gr.Dropdown(
                    label="Model",
                    choices=[
                        "mistralai/Mixtral-8x7B-Instruct-v0.1",
                        "mistralai/Mistral-7B-Instruct-v0.2"
                    ],
                    value="mistralai/Mixtral-8x7B-Instruct-v0.1"
                )
                generate_btn = gr.Button("Generate Story")
            
            with gr.Row():
                story_output = gr.Textbox(
                    label="Generated Story",
                    lines=10,
                    interactive=False
                )
            
            with gr.Row():
                audio_output = gr.Audio(
                    label="Story Narration",
                    type="filepath"
                )
        
        # Sidebar with Story Starters and Gallery
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“š Story Starters")
            story_starters = gr.Dataframe(
                value=STORY_STARTERS,
                headers=["Category", "Starter"],
                interactive=False
            )
            
            gr.Markdown("### 🎬 Gallery")
            gallery = gr.Dataframe(
                value=load_gallery(),
                headers=["Timestamp", "Preview", "Story Path", "Audio Path"],
                interactive=False
            )
    
    # Event handlers
    def update_prompt(evt: gr.SelectData):
        return STORY_STARTERS[evt.index[0]][1]
    
    story_starters.select(update_prompt, None, prompt_input)
    
    generate_btn.click(
        fn=process_story_and_audio,
        inputs=[prompt_input, model_choice],
        outputs=[story_output, audio_output, gallery]
    )
    
    gallery.select(
        fn=play_gallery_audio,
        inputs=[gallery],
        outputs=[audio_output]
    )

if __name__ == "__main__":
    demo.launch()