orrinin commited on
Commit
561aac5
·
verified ·
1 Parent(s): 71debf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -5
app.py CHANGED
@@ -3,7 +3,9 @@ import json
3
  import httpx
4
  import os
5
  import re
 
6
  import asyncio
 
7
  import edge_tts
8
  import tempfile
9
  import gradio as gr
@@ -13,18 +15,18 @@ from pydub import AudioSegment
13
  from moviepy.editor import AudioFileClip, concatenate_audioclips
14
 
15
  system_prompt = '''
16
- You are an talk-show podcast generator. You have to create short conversations between Xiaoxiao and Yunjian that gives an overview of the News given by the user.
17
  Please provide the script and output strictly in the following JSON format:
18
  {
19
  "title": "[string]",
20
  "content": {
21
- "Xiaoxiao_0: "[string]",
22
- "Yunjian_0": "[string]",
23
  ...
24
  }
25
  }
26
  #Please note that the [string] you generate now must be in easy-and-understandable Chinese.
27
- #Be concise. No less than three rounds of conversation.
28
  '''
29
 
30
  DESCRIPTION = '''
@@ -49,10 +51,39 @@ footer {
49
  }
50
  """
51
 
 
 
 
52
  apikey = os.environ.get("API_KEY")
53
  client = OpenAI(api_key=apikey, base_url="https://api.deepseek.com")
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def validate_url(url):
57
  try:
58
  response = httpx.get(url, timeout=60.0)
@@ -73,6 +104,7 @@ def fetch_text(url):
73
  print("Exited Webpage Extraction")
74
  return validate_url(full_url)
75
 
 
76
  async def text_to_speech(text, voice, filename):
77
  communicate = edge_tts.Communicate(text, voice)
78
  await communicate.save(filename)
@@ -88,7 +120,7 @@ async def gen_show(script):
88
  for key, text in content.items():
89
  speaker = key.split('_')[0] # Extract the speaker name
90
  index = key.split('_')[1] # Extract the dialogue index
91
- voice = "zh-CN-XiaoxiaoNeural" if speaker == "Xiaoxiao" else "zh-CN-YunjianNeural"
92
 
93
  # Create temporary file for each speaker's dialogue
94
  temp_file = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False)
@@ -178,11 +210,13 @@ with gr.Blocks(theme='soft', css=css, title="听说") as iface:
178
  input_box = gr.Textbox(label="网址", placeholder="请输入https开头的网址")
179
  with gr.Row():
180
  submit_btn = gr.Button("🚀 发送") # Create a submit button
 
181
  clear_btn = gr.ClearButton(output_box, value="🗑️ 清除") # Create a clear button
182
  gr.Examples(examples=Examples, inputs=input_box, outputs=output_box, fn=main, label="示例", cache_examples="lazy")
183
 
184
  # Set up the event listeners
185
  submit_btn.click(main, inputs=input_box, outputs=output_box)
 
186
 
187
 
188
  #gr.close_all()
 
3
  import httpx
4
  import os
5
  import re
6
+ import feedparser
7
  import asyncio
8
+ import random
9
  import edge_tts
10
  import tempfile
11
  import gradio as gr
 
15
  from moviepy.editor import AudioFileClip, concatenate_audioclips
16
 
17
  system_prompt = '''
18
+ You are an news podcast generator. You have to create short conversations between Chen and Yun that gives an overview of the News given by the user.
19
  Please provide the script and output strictly in the following JSON format:
20
  {
21
  "title": "[string]",
22
  "content": {
23
+ "Chen_0: "[string]",
24
+ "Yun_0": "[string]",
25
  ...
26
  }
27
  }
28
  #Please note that the [string] you generate now must be in easy-and-understandable Chinese.
29
+ #Be concise. No less than five rounds of conversation.
30
  '''
31
 
32
  DESCRIPTION = '''
 
51
  }
52
  """
53
 
54
+ rss_feed = 'https://www.yahoo.com/news/rss'
55
+
56
+
57
  apikey = os.environ.get("API_KEY")
58
  client = OpenAI(api_key=apikey, base_url="https://api.deepseek.com")
59
 
60
 
61
+
62
+ # RSS feeds
63
+
64
+ def is_url(string):
65
+ url_pattern = re.compile(
66
+ r'^(?:http|ftp)s?://' # http:// or https://
67
+ r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
68
+ r'localhost|' # localhost...
69
+ r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
70
+ r'(?::\d+)?' # optional port
71
+ r'(?:/?|[/?]\S+)$', re.IGNORECASE)
72
+ return re.match(url_pattern, string) is not None
73
+
74
+ def random_news():
75
+ global rss_feed
76
+ if not is_url(rss_feed):
77
+ raise ValueError(f"{rss_feed} is not a valid RSS feed.")
78
+ news = []
79
+ feed = feedparser.parse(rss_feed)
80
+ for entry in feed.entries:
81
+ news.append(entry.title)
82
+ print(news)
83
+ main(random.choice(news))
84
+
85
+
86
+
87
  def validate_url(url):
88
  try:
89
  response = httpx.get(url, timeout=60.0)
 
104
  print("Exited Webpage Extraction")
105
  return validate_url(full_url)
106
 
107
+
108
  async def text_to_speech(text, voice, filename):
109
  communicate = edge_tts.Communicate(text, voice)
110
  await communicate.save(filename)
 
120
  for key, text in content.items():
121
  speaker = key.split('_')[0] # Extract the speaker name
122
  index = key.split('_')[1] # Extract the dialogue index
123
+ voice = "zh-TW-HsiaoYuNeural" if speaker == "Chen" else "zh-CN-YunyangNeural"
124
 
125
  # Create temporary file for each speaker's dialogue
126
  temp_file = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False)
 
210
  input_box = gr.Textbox(label="网址", placeholder="请输入https开头的网址")
211
  with gr.Row():
212
  submit_btn = gr.Button("🚀 发送") # Create a submit button
213
+ random_btn = gr.Button("🤙 随机")
214
  clear_btn = gr.ClearButton(output_box, value="🗑️ 清除") # Create a clear button
215
  gr.Examples(examples=Examples, inputs=input_box, outputs=output_box, fn=main, label="示例", cache_examples="lazy")
216
 
217
  # Set up the event listeners
218
  submit_btn.click(main, inputs=input_box, outputs=output_box)
219
+ random_btn.click(fn=random_news, outputs=output_box)
220
 
221
 
222
  #gr.close_all()