Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
|
|
5 |
import openai
|
6 |
import os
|
7 |
import re
|
|
|
|
|
8 |
def create_client(api_key=None):
|
9 |
if api_key:
|
10 |
openai.api_key = api_key
|
@@ -76,7 +78,29 @@ Follow this example structure, MUST be in {language} language:
|
|
76 |
)
|
77 |
try:
|
78 |
podcast_match = re.search(r'{(.*?)}', response.choices[0].message.content, re.DOTALL)
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
except Exception as e:
|
81 |
if "API key not valid" in str(e):
|
82 |
raise gr.Error("Invalid API key. Please provide a valid API key.")
|
@@ -84,12 +108,11 @@ Follow this example structure, MUST be in {language} language:
|
|
84 |
raise gr.Error("Rate limit exceeded for the API key. Please try again later or provide your own API key.")
|
85 |
else:
|
86 |
raise gr.Error(f"Failed to generate podcast script: {e}")
|
87 |
-
return response.choices[0].message.content
|
88 |
|
89 |
with gr.Blocks() as iface:
|
90 |
gr.Markdown("# 🎙️ Generated Podcast Audio. Deployed by 江信宗")
|
91 |
|
92 |
-
input_text = gr.Textbox(label="
|
93 |
|
94 |
with gr.Row():
|
95 |
Language = gr.Dropdown(
|
|
|
5 |
import openai
|
6 |
import os
|
7 |
import re
|
8 |
+
import json
|
9 |
+
|
10 |
def create_client(api_key=None):
|
11 |
if api_key:
|
12 |
openai.api_key = api_key
|
|
|
78 |
)
|
79 |
try:
|
80 |
podcast_match = re.search(r'{(.*?)}', response.choices[0].message.content, re.DOTALL)
|
81 |
+
if podcast_match:
|
82 |
+
podcast_content = podcast_match.group(0)
|
83 |
+
podcast_dict = json.loads(podcast_content)
|
84 |
+
|
85 |
+
# 創建新的 JSON 格式
|
86 |
+
podcast_json = {
|
87 |
+
"topic": input_text[:30] + "..." if len(input_text) > 30 else input_text,
|
88 |
+
"podcast": []
|
89 |
+
}
|
90 |
+
|
91 |
+
speaker_map = {"speaker1": 1, "speaker2": 2}
|
92 |
+
|
93 |
+
for speaker, line in podcast_dict.items():
|
94 |
+
podcast_json["podcast"].append({
|
95 |
+
"speaker": speaker_map[speaker],
|
96 |
+
"line": line
|
97 |
+
})
|
98 |
+
|
99 |
+
return json.dumps(podcast_json, ensure_ascii=False, indent=2)
|
100 |
+
else:
|
101 |
+
raise gr.Error("Failed to generate podcast script. Please try again.")
|
102 |
+
except json.JSONDecodeError:
|
103 |
+
raise gr.Error("Invalid JSON format in the generated response. Please try again.")
|
104 |
except Exception as e:
|
105 |
if "API key not valid" in str(e):
|
106 |
raise gr.Error("Invalid API key. Please provide a valid API key.")
|
|
|
108 |
raise gr.Error("Rate limit exceeded for the API key. Please try again later or provide your own API key.")
|
109 |
else:
|
110 |
raise gr.Error(f"Failed to generate podcast script: {e}")
|
|
|
111 |
|
112 |
with gr.Blocks() as iface:
|
113 |
gr.Markdown("# 🎙️ Generated Podcast Audio. Deployed by 江信宗")
|
114 |
|
115 |
+
input_text = gr.Textbox(label="請輸入 Podcast 話題(建議50~500字之間)")
|
116 |
|
117 |
with gr.Row():
|
118 |
Language = gr.Dropdown(
|