Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -376,14 +376,97 @@
|
|
376 |
# if __name__ == "__main__":
|
377 |
# app.launch()
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
import random
|
380 |
import gradio as gr
|
381 |
-
from transformers import pipeline
|
382 |
from TTS.api import TTS
|
383 |
|
384 |
# --------------- Lightweight Models ---------------
|
385 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
386 |
-
|
|
|
|
|
387 |
tts = TTS(model_name="tts_models/en/ljspeech/glow-tts", progress_bar=False, gpu=False)
|
388 |
|
389 |
# --------------- Feature 1: Daily Audio Digest ---------------
|
@@ -397,6 +480,7 @@ def generate_audio_digest(topic):
|
|
397 |
return summary, audio_path, audio_path
|
398 |
|
399 |
# --------------- Feature 2: Interactive Story Generator ---------------
|
|
|
400 |
genre_templates = {
|
401 |
"fantasy": "In a land of dragons and forgotten magic, a tale begins.",
|
402 |
"sci-fi": "In a distant galaxy governed by AI and cosmic laws, a new story unfolds.",
|
@@ -411,22 +495,25 @@ def generate_story(genre, choice):
|
|
411 |
genre = genre.lower()
|
412 |
base_prompt = genre_templates.get(genre, f"Begin a story in the genre: {genre}.")
|
413 |
|
|
|
414 |
prompt = base_prompt
|
415 |
if choice:
|
416 |
-
prompt += f" The
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
|
|
|
|
|
|
421 |
next_scene = generated[len(prompt):].strip()
|
422 |
-
|
423 |
story_audio_path = "story.wav"
|
424 |
tts.tts_to_file(text=next_scene, file_path=story_audio_path)
|
425 |
|
426 |
return next_scene, story_audio_path, story_audio_path
|
427 |
|
428 |
# --------------- Gradio UI ---------------
|
429 |
-
|
430 |
digest_ui = gr.Interface(
|
431 |
fn=generate_audio_digest,
|
432 |
inputs=gr.Textbox(label="🎯 Enter your topic of interest", placeholder="e.g. motivation, productivity", lines=2),
|
@@ -441,7 +528,7 @@ digest_ui = gr.Interface(
|
|
441 |
story_ui = gr.Interface(
|
442 |
fn=generate_story,
|
443 |
inputs=[
|
444 |
-
gr.Textbox(label="📚 Genre", placeholder="e.g. sci-fi, myth, thriller", lines=1),
|
445 |
gr.Textbox(label="🎮 Your last choice", placeholder="e.g. Enter the castle", lines=2)
|
446 |
],
|
447 |
outputs=[
|
|
|
376 |
# if __name__ == "__main__":
|
377 |
# app.launch()
|
378 |
|
379 |
+
# import random
|
380 |
+
# import gradio as gr
|
381 |
+
# from transformers import pipeline
|
382 |
+
# from TTS.api import TTS
|
383 |
+
|
384 |
+
# # --------------- Lightweight Models ---------------
|
385 |
+
# summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
386 |
+
# story_gen = pipeline("text-generation", model="mrm8488/GPT-2-finetuned-fiction", max_length=250, pad_token_id=50256)
|
387 |
+
# tts = TTS(model_name="tts_models/en/ljspeech/glow-tts", progress_bar=False, gpu=False)
|
388 |
+
|
389 |
+
# # --------------- Feature 1: Daily Audio Digest ---------------
|
390 |
+
# def generate_audio_digest(topic):
|
391 |
+
# dummy_text = f"This is a sample Kuku FM podcast about {topic}. " * 10
|
392 |
+
# summary = summarizer(dummy_text, max_length=300, min_length=30, do_sample=False)[0]["summary_text"]
|
393 |
+
|
394 |
+
# audio_path = "digest.wav"
|
395 |
+
# tts.tts_to_file(text=summary, file_path=audio_path)
|
396 |
+
|
397 |
+
# return summary, audio_path, audio_path
|
398 |
+
|
399 |
+
# # --------------- Feature 2: Interactive Story Generator ---------------
|
400 |
+
# genre_templates = {
|
401 |
+
# "fantasy": "In a land of dragons and forgotten magic, a tale begins.",
|
402 |
+
# "sci-fi": "In a distant galaxy governed by AI and cosmic laws, a new story unfolds.",
|
403 |
+
# "thriller": "It started on a stormy night, when a knock on the door changed everything.",
|
404 |
+
# "romance": "They locked eyes for the first time in a quiet bookstore.",
|
405 |
+
# "horror": "The clock struck midnight, and something moved in the shadows.",
|
406 |
+
# "myth": "Long before time began, gods and mortals shared the same sky.",
|
407 |
+
# "comedy": "There was once a chicken who wanted to cross a space highway..."
|
408 |
+
# }
|
409 |
+
|
410 |
+
# def generate_story(genre, choice):
|
411 |
+
# genre = genre.lower()
|
412 |
+
# base_prompt = genre_templates.get(genre, f"Begin a story in the genre: {genre}.")
|
413 |
+
|
414 |
+
# prompt = base_prompt
|
415 |
+
# if choice:
|
416 |
+
# prompt += f" The character decided to '{choice}'."
|
417 |
+
|
418 |
+
# prompt += " What happened next was"
|
419 |
+
|
420 |
+
# generated = story_gen(prompt, do_sample=True, top_p=0.92, temperature=0.9)[0]['generated_text']
|
421 |
+
# next_scene = generated[len(prompt):].strip()
|
422 |
+
|
423 |
+
# story_audio_path = "story.wav"
|
424 |
+
# tts.tts_to_file(text=next_scene, file_path=story_audio_path)
|
425 |
+
|
426 |
+
# return next_scene, story_audio_path, story_audio_path
|
427 |
+
|
428 |
+
# # --------------- Gradio UI ---------------
|
429 |
+
|
430 |
+
# digest_ui = gr.Interface(
|
431 |
+
# fn=generate_audio_digest,
|
432 |
+
# inputs=gr.Textbox(label="🎯 Enter your topic of interest", placeholder="e.g. motivation, productivity", lines=2),
|
433 |
+
# outputs=[
|
434 |
+
# gr.Textbox(label="📝 Summary", lines=4),
|
435 |
+
# gr.Audio(label="🎧 Audio Digest", type="filepath"),
|
436 |
+
# gr.File(label="⬇️ Download Audio")
|
437 |
+
# ],
|
438 |
+
# title="🎧 KukuBuddy: Daily Audio Digest"
|
439 |
+
# )
|
440 |
+
|
441 |
+
# story_ui = gr.Interface(
|
442 |
+
# fn=generate_story,
|
443 |
+
# inputs=[
|
444 |
+
# gr.Textbox(label="📚 Genre", placeholder="e.g. sci-fi, myth, thriller", lines=1),
|
445 |
+
# gr.Textbox(label="🎮 Your last choice", placeholder="e.g. Enter the castle", lines=2)
|
446 |
+
# ],
|
447 |
+
# outputs=[
|
448 |
+
# gr.Textbox(label="📝 Next Scene", lines=4),
|
449 |
+
# gr.Audio(label="🎧 Narration", type="filepath"),
|
450 |
+
# gr.File(label="⬇️ Download Audio")
|
451 |
+
# ],
|
452 |
+
# title="📖 KukuBuddy: Interactive Story"
|
453 |
+
# )
|
454 |
+
|
455 |
+
# app = gr.TabbedInterface([digest_ui, story_ui], tab_names=["📌 Daily Digest", "🧠 Interactive Story"])
|
456 |
+
|
457 |
+
# if __name__ == "__main__":
|
458 |
+
# app.launch()
|
459 |
+
|
460 |
import random
|
461 |
import gradio as gr
|
462 |
+
from transformers import pipeline, set_seed
|
463 |
from TTS.api import TTS
|
464 |
|
465 |
# --------------- Lightweight Models ---------------
|
466 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
467 |
+
# Use GPT-Neo 125M for narrative generation
|
468 |
+
story_gen = pipeline("text-generation", model="EleutherAI/gpt-neo-125M", max_length=250, pad_token_id=50256)
|
469 |
+
set_seed(42)
|
470 |
tts = TTS(model_name="tts_models/en/ljspeech/glow-tts", progress_bar=False, gpu=False)
|
471 |
|
472 |
# --------------- Feature 1: Daily Audio Digest ---------------
|
|
|
480 |
return summary, audio_path, audio_path
|
481 |
|
482 |
# --------------- Feature 2: Interactive Story Generator ---------------
|
483 |
+
# Genre-specific openers to guide the style of the story
|
484 |
genre_templates = {
|
485 |
"fantasy": "In a land of dragons and forgotten magic, a tale begins.",
|
486 |
"sci-fi": "In a distant galaxy governed by AI and cosmic laws, a new story unfolds.",
|
|
|
495 |
genre = genre.lower()
|
496 |
base_prompt = genre_templates.get(genre, f"Begin a story in the genre: {genre}.")
|
497 |
|
498 |
+
# Build a more detailed prompt using the user's choice.
|
499 |
prompt = base_prompt
|
500 |
if choice:
|
501 |
+
prompt += f" The protagonist made a bold decision: '{choice}'."
|
502 |
+
|
503 |
+
# Add clear instructions to generate a coherent narrative.
|
504 |
+
prompt += " Continue the story in a creative and engaging way. Develop the characters and emotions naturally."
|
505 |
+
|
506 |
+
generated = story_gen(prompt, do_sample=True, top_p=0.9, temperature=0.85)[0]['generated_text']
|
507 |
+
|
508 |
+
# Extract only the generated continuation.
|
509 |
next_scene = generated[len(prompt):].strip()
|
510 |
+
|
511 |
story_audio_path = "story.wav"
|
512 |
tts.tts_to_file(text=next_scene, file_path=story_audio_path)
|
513 |
|
514 |
return next_scene, story_audio_path, story_audio_path
|
515 |
|
516 |
# --------------- Gradio UI ---------------
|
|
|
517 |
digest_ui = gr.Interface(
|
518 |
fn=generate_audio_digest,
|
519 |
inputs=gr.Textbox(label="🎯 Enter your topic of interest", placeholder="e.g. motivation, productivity", lines=2),
|
|
|
528 |
story_ui = gr.Interface(
|
529 |
fn=generate_story,
|
530 |
inputs=[
|
531 |
+
gr.Textbox(label="📚 Genre", placeholder="e.g. sci-fi, myth, thriller, romance", lines=1),
|
532 |
gr.Textbox(label="🎮 Your last choice", placeholder="e.g. Enter the castle", lines=2)
|
533 |
],
|
534 |
outputs=[
|