m-ric HF Staff commited on
Commit
00809b2
Β·
1 Parent(s): 4d88a72

Working streaming and cool looking app

Browse files
Files changed (2) hide show
  1. app.py +28 -23
  2. prompts.py +3 -5
app.py CHANGED
@@ -7,12 +7,14 @@ import soundfile as sf
7
  import gradio as gr
8
  import numpy as np
9
  import time
 
 
10
 
11
  import torch
12
  from huggingface_hub import InferenceClient
13
  from kokoro import KModel, KPipeline
14
  # -----------------------------------------------------------------------------
15
- # Get podcast subject
16
  # -----------------------------------------------------------------------------
17
  from papers import PaperManager
18
 
@@ -33,12 +35,12 @@ client = InferenceClient(
33
  )
34
 
35
 
36
- def generate_podcast_text(subject: str, steering_question: str | None = None) -> str:
37
  """Ask the LLM for a script of a podcast given by two hosts."""
38
  messages = [
39
  {"role": "system", "content": SYSTEM_PROMPT},
40
  {"role": "user", "content": f"""Here is the topic: it's the top trending paper on Hugging Face daily papers today. You will need to analyze it by bringing profound insights.
41
- {subject[:1000]}"""},
42
  ]
43
  if steering_question and len(steering_question) > 0:
44
  messages.append({"role": "user", "content": f"You could focus on this question: {steering_question}"})
@@ -58,7 +60,7 @@ def generate_podcast_text(subject: str, steering_question: str | None = None) ->
58
  # -----------------------------------------------------------------------------
59
  CUDA_AVAILABLE = torch.cuda.is_available()
60
 
61
- kmodel = KModel().to("cuda" if CUDA_AVAILABLE else "cpu").eval()
62
  kpipeline = KPipeline(lang_code="a") # English voices
63
 
64
  MALE_VOICE = "am_michael" # [MIKE]
@@ -68,14 +70,23 @@ FEMALE_VOICE = "af_heart" # [JANE]
68
  for v in (MALE_VOICE, FEMALE_VOICE):
69
  kpipeline.load_voice(v)
70
 
71
- # -----------------------------------------------------------------------------
72
- # Audio generation system with queue
73
- # -----------------------------------------------------------------------------
74
-
75
  @spaces.GPU
76
- def generate_podcast(pdf, url, topic):
77
- podcast_text = generate_podcast_text(PODCAST_SUBJECT, topic)
78
- lines = [l for l in podcast_text.strip().splitlines() if l.strip()]
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  pipeline = kpipeline
81
  pipeline_voice_female = pipeline.load_voice(FEMALE_VOICE)
@@ -114,28 +125,22 @@ demo = gr.Interface(
114
  If you do not specify any source materials below, the podcast will be about the top trending [Daily paper](https://huggingface.co/papers/), '**{list(top_papers.keys())[0]}**'""",
115
  fn=generate_podcast,
116
  inputs=[
 
 
 
117
  gr.File(
118
- label="Optional - Upload a pdf",
119
  file_types=[".pdf"],
120
  file_count="single",
121
  ),
122
- gr.Textbox(
123
- label="Optional - Type a URL to read its page",
124
- ),
125
- gr.Textbox(label="Do you have a more specific topic or question on the materials?"),
126
- # gr.Dropdown(
127
- # label=UI_INPUTS["length"]["label"],
128
- # choices=UI_INPUTS["length"]["choices"],
129
- # value=UI_INPUTS["length"]["value"],
130
- # ),
131
  ],
132
  outputs=[
133
  gr.Audio(
134
- label="Listen to your podcast",
135
  format="wav",
136
  streaming=True,
137
  ),
138
- # gr.Markdown(label=UI_OUTPUTS["transcript"]["label"]),
139
  ],
140
  theme=gr.themes.Soft(),
141
  submit_btn="Generate podcast πŸŽ™οΈ",
 
7
  import gradio as gr
8
  import numpy as np
9
  import time
10
+ import pymupdf
11
+ import requests
12
 
13
  import torch
14
  from huggingface_hub import InferenceClient
15
  from kokoro import KModel, KPipeline
16
  # -----------------------------------------------------------------------------
17
+ # Get default podcast materials from Daily papers
18
  # -----------------------------------------------------------------------------
19
  from papers import PaperManager
20
 
 
35
  )
36
 
37
 
38
+ def generate_podcast_script(subject: str, steering_question: str | None = None) -> str:
39
  """Ask the LLM for a script of a podcast given by two hosts."""
40
  messages = [
41
  {"role": "system", "content": SYSTEM_PROMPT},
42
  {"role": "user", "content": f"""Here is the topic: it's the top trending paper on Hugging Face daily papers today. You will need to analyze it by bringing profound insights.
43
+ {subject[:10000]}"""},
44
  ]
45
  if steering_question and len(steering_question) > 0:
46
  messages.append({"role": "user", "content": f"You could focus on this question: {steering_question}"})
 
60
  # -----------------------------------------------------------------------------
61
  CUDA_AVAILABLE = torch.cuda.is_available()
62
 
63
+ kmodel = KModel(repo_id='hexgrad/Kokoro-82M').to("cuda" if CUDA_AVAILABLE else "cpu").eval()
64
  kpipeline = KPipeline(lang_code="a") # English voices
65
 
66
  MALE_VOICE = "am_michael" # [MIKE]
 
70
  for v in (MALE_VOICE, FEMALE_VOICE):
71
  kpipeline.load_voice(v)
72
 
 
 
 
 
73
  @spaces.GPU
74
+ def generate_podcast(url: str, pdf_path: str, topic: str):
75
+ if pdf_path:
76
+ with pymupdf.open(pdf_path) as pdf_doc:
77
+ material_text = ""
78
+ for page in pdf_doc:
79
+ material_text += page.get_text()
80
+ elif url:
81
+ response = requests.get(f'https://r.jina.ai/{url}')
82
+ material_text = response.text
83
+ else:
84
+ material_text = PODCAST_SUBJECT
85
+
86
+ # Generate podcast script!
87
+ podcast_script = generate_podcast_script(material_text, topic)
88
+
89
+ lines = [l for l in podcast_script.strip().splitlines() if l.strip()]
90
 
91
  pipeline = kpipeline
92
  pipeline_voice_female = pipeline.load_voice(FEMALE_VOICE)
 
125
  If you do not specify any source materials below, the podcast will be about the top trending [Daily paper](https://huggingface.co/papers/), '**{list(top_papers.keys())[0]}**'""",
126
  fn=generate_podcast,
127
  inputs=[
128
+ gr.Textbox(
129
+ label="πŸ”— Type a Webpage URL to discuss this page (Optional)",
130
+ ),
131
  gr.File(
132
+ label="πŸ“„ Upload a PDF to use it as discussion material (Optional)",
133
  file_types=[".pdf"],
134
  file_count="single",
135
  ),
136
+ gr.Textbox(label="πŸ€” Do you have a more specific topic or question on the materials?", placeholder="You can leave this blank."),
 
 
 
 
 
 
 
 
137
  ],
138
  outputs=[
139
  gr.Audio(
140
+ label="Listen to your podcast πŸ”Š",
141
  format="wav",
142
  streaming=True,
143
  ),
 
144
  ],
145
  theme=gr.themes.Soft(),
146
  submit_btn="Generate podcast πŸŽ™οΈ",
prompts.py CHANGED
@@ -46,10 +46,8 @@ Ensure the dialogue has a natural ebb and flow:
46
 
47
  IMPORTANT RULE: Each line of dialogue should go in a new line [JANE] or [MIKE], as follows:
48
 
49
- [JANE] Hello, how are you?
50
- [MIKE] I'm good, thank you. How are you?
51
- [JANE] I'm good, thank you.
52
- [MIKE] Great.
53
 
54
- Remember: Each intervention from a host should be on the same line.
55
  """
 
46
 
47
  IMPORTANT RULE: Each line of dialogue should go in a new line [JANE] or [MIKE], as follows:
48
 
49
+ [JANE] Hello Mike, how are you?
50
+ [MIKE] Nice to see you again, Jane. I'm very good. Today's topic is fascinating, because...
 
 
51
 
52
+ Remember: Each turn from a host should be on the same line.
53
  """