Spaces:
Runtime error
Runtime error
knowsuchagency
commited on
Commit
•
8729c75
1
Parent(s):
e19d77c
allow the gemini api key to be passed via gradio
Browse files
main.py
CHANGED
@@ -44,42 +44,6 @@ class Dialogue(BaseModel):
|
|
44 |
dialogue: List[DialogueItem]
|
45 |
|
46 |
|
47 |
-
@retry(retry=retry_if_exception_type(ValidationError))
|
48 |
-
@llm(model="gemini/gemini-1.5-flash", max_tokens=8192)
|
49 |
-
def generate_dialogue(text: str) -> Dialogue:
|
50 |
-
"""
|
51 |
-
Your task is to take the input text provided and turn it into an engaging, informative podcast dialogue. The input text may be messy or unstructured, as it could come from a variety of sources like PDFs or web pages. Don't worry about the formatting issues or any irrelevant information; your goal is to extract the key points and interesting facts that could be discussed in a podcast.
|
52 |
-
|
53 |
-
Here is the input text you will be working with:
|
54 |
-
|
55 |
-
<input_text>
|
56 |
-
{text}
|
57 |
-
</input_text>
|
58 |
-
|
59 |
-
First, carefully read through the input text and identify the main topics, key points, and any interesting facts or anecdotes. Think about how you could present this information in a fun, engaging way that would be suitable for an audio podcast.
|
60 |
-
|
61 |
-
<scratchpad>
|
62 |
-
Brainstorm creative ways to discuss the main topics and key points you identified in the input text. Consider using analogies, storytelling techniques, or hypothetical scenarios to make the content more relatable and engaging for listeners.
|
63 |
-
|
64 |
-
Keep in mind that your podcast should be accessible to a general audience, so avoid using too much jargon or assuming prior knowledge of the topic. If necessary, think of ways to briefly explain any complex concepts in simple terms.
|
65 |
-
|
66 |
-
Use your imagination to fill in any gaps in the input text or to come up with thought-provoking questions that could be explored in the podcast. The goal is to create an informative and entertaining dialogue, so feel free to be creative in your approach.
|
67 |
-
|
68 |
-
Write your brainstorming ideas and a rough outline for the podcast dialogue here. Be sure to note the key insights and takeaways you want to reiterate at the end.
|
69 |
-
</scratchpad>
|
70 |
-
|
71 |
-
Now that you have brainstormed ideas and created a rough outline, it's time to write the actual podcast dialogue. Aim for a natural, conversational flow between the host and any guest speakers. Incorporate the best ideas from your brainstorming session and make sure to explain any complex topics in an easy-to-understand way.
|
72 |
-
|
73 |
-
<podcast_dialogue>
|
74 |
-
Write your engaging, informative podcast dialogue here, based on the key points and creative ideas you came up with during the brainstorming session. Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience. Use made-up names for the hosts and guests to create a more engaging and immersive experience for listeners. Do not include any bracketed placeholders like [Host] or [Guest]. Design your output to be read aloud -- it will be directly converted into audio.
|
75 |
-
|
76 |
-
Make the dialogue as long and detailed as possible, while still staying on topic and maintaining an engaging flow. Aim to use your full output capacity to create the longest podcast episode you can, while still communicating the key information from the input text in an entertaining way.
|
77 |
-
|
78 |
-
At the end of the dialogue, have the host and guest speakers naturally summarize the main insights and takeaways from their discussion. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner. Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas one last time before signing off.
|
79 |
-
</podcast_dialogue>
|
80 |
-
"""
|
81 |
-
|
82 |
-
|
83 |
def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
|
84 |
client = OpenAI(
|
85 |
api_key=api_key or os.getenv("OPENAI_API_KEY"),
|
@@ -96,15 +60,57 @@ def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
|
|
96 |
return file.getvalue()
|
97 |
|
98 |
|
99 |
-
def generate_audio(
|
|
|
|
|
|
|
|
|
100 |
|
101 |
if not os.getenv("OPENAI_API_KEY", openai_api_key):
|
102 |
raise gr.Error("OpenAI API key is required")
|
|
|
|
|
|
|
103 |
|
104 |
with Path(file).open("rb") as f:
|
105 |
reader = PdfReader(f)
|
106 |
text = "\n\n".join([page.extract_text() for page in reader.pages])
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
llm_output = generate_dialogue(text)
|
109 |
|
110 |
audio = b""
|
@@ -160,6 +166,10 @@ demo = gr.Interface(
|
|
160 |
label="OpenAI API Key",
|
161 |
visible=not os.getenv("OPENAI_API_KEY"),
|
162 |
),
|
|
|
|
|
|
|
|
|
163 |
],
|
164 |
outputs=[
|
165 |
gr.Audio(label="Audio", format="mp3"),
|
|
|
44 |
dialogue: List[DialogueItem]
|
45 |
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
|
48 |
client = OpenAI(
|
49 |
api_key=api_key or os.getenv("OPENAI_API_KEY"),
|
|
|
60 |
return file.getvalue()
|
61 |
|
62 |
|
63 |
+
def generate_audio(
|
64 |
+
file: str,
|
65 |
+
openai_api_key: str = None,
|
66 |
+
gemini_api_key: str = None,
|
67 |
+
) -> bytes:
|
68 |
|
69 |
if not os.getenv("OPENAI_API_KEY", openai_api_key):
|
70 |
raise gr.Error("OpenAI API key is required")
|
71 |
+
|
72 |
+
if not os.getenv("GEMINI_API_KEY", gemini_api_key):
|
73 |
+
raise gr.Error("Gemini API key is required")
|
74 |
|
75 |
with Path(file).open("rb") as f:
|
76 |
reader = PdfReader(f)
|
77 |
text = "\n\n".join([page.extract_text() for page in reader.pages])
|
78 |
|
79 |
+
@retry(retry=retry_if_exception_type(ValidationError))
|
80 |
+
@llm(model="gemini/gemini-1.5-flash", max_tokens=8192, api_key=gemini_api_key)
|
81 |
+
def generate_dialogue(text: str) -> Dialogue:
|
82 |
+
"""
|
83 |
+
Your task is to take the input text provided and turn it into an engaging, informative podcast dialogue. The input text may be messy or unstructured, as it could come from a variety of sources like PDFs or web pages. Don't worry about the formatting issues or any irrelevant information; your goal is to extract the key points and interesting facts that could be discussed in a podcast.
|
84 |
+
|
85 |
+
Here is the input text you will be working with:
|
86 |
+
|
87 |
+
<input_text>
|
88 |
+
{text}
|
89 |
+
</input_text>
|
90 |
+
|
91 |
+
First, carefully read through the input text and identify the main topics, key points, and any interesting facts or anecdotes. Think about how you could present this information in a fun, engaging way that would be suitable for an audio podcast.
|
92 |
+
|
93 |
+
<scratchpad>
|
94 |
+
Brainstorm creative ways to discuss the main topics and key points you identified in the input text. Consider using analogies, storytelling techniques, or hypothetical scenarios to make the content more relatable and engaging for listeners.
|
95 |
+
|
96 |
+
Keep in mind that your podcast should be accessible to a general audience, so avoid using too much jargon or assuming prior knowledge of the topic. If necessary, think of ways to briefly explain any complex concepts in simple terms.
|
97 |
+
|
98 |
+
Use your imagination to fill in any gaps in the input text or to come up with thought-provoking questions that could be explored in the podcast. The goal is to create an informative and entertaining dialogue, so feel free to be creative in your approach.
|
99 |
+
|
100 |
+
Write your brainstorming ideas and a rough outline for the podcast dialogue here. Be sure to note the key insights and takeaways you want to reiterate at the end.
|
101 |
+
</scratchpad>
|
102 |
+
|
103 |
+
Now that you have brainstormed ideas and created a rough outline, it's time to write the actual podcast dialogue. Aim for a natural, conversational flow between the host and any guest speakers. Incorporate the best ideas from your brainstorming session and make sure to explain any complex topics in an easy-to-understand way.
|
104 |
+
|
105 |
+
<podcast_dialogue>
|
106 |
+
Write your engaging, informative podcast dialogue here, based on the key points and creative ideas you came up with during the brainstorming session. Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience. Use made-up names for the hosts and guests to create a more engaging and immersive experience for listeners. Do not include any bracketed placeholders like [Host] or [Guest]. Design your output to be read aloud -- it will be directly converted into audio.
|
107 |
+
|
108 |
+
Make the dialogue as long and detailed as possible, while still staying on topic and maintaining an engaging flow. Aim to use your full output capacity to create the longest podcast episode you can, while still communicating the key information from the input text in an entertaining way.
|
109 |
+
|
110 |
+
At the end of the dialogue, have the host and guest speakers naturally summarize the main insights and takeaways from their discussion. This should flow organically from the conversation, reiterating the key points in a casual, conversational manner. Avoid making it sound like an obvious recap - the goal is to reinforce the central ideas one last time before signing off.
|
111 |
+
</podcast_dialogue>
|
112 |
+
"""
|
113 |
+
|
114 |
llm_output = generate_dialogue(text)
|
115 |
|
116 |
audio = b""
|
|
|
166 |
label="OpenAI API Key",
|
167 |
visible=not os.getenv("OPENAI_API_KEY"),
|
168 |
),
|
169 |
+
gr.Textbox(
|
170 |
+
label="Gemini API Key",
|
171 |
+
visible=not os.getenv("GEMINI_API_KEY"),
|
172 |
+
),
|
173 |
],
|
174 |
outputs=[
|
175 |
gr.Audio(label="Audio", format="mp3"),
|