Skip to content

Forum in maintenance, we will back soon 🙂

More Workflow Ideas...
 
Notifications
Clear all

More Workflow Ideas - podcast2blog

4 Posts
3 Users
3 Reactions
17 Views
(@hagai-ben-moshe)
Posts: 6
Active Member Customer Registered
Topic starter
 

Hello everyone, I'm trying to implement the ideas in this part of the lesson. Of course I used the chat and succeeded after an effort to generate a code that receives a URL of a podcast, extracts the transcription and sends it to LLM and from there to create a blog. here the code(For those who want to try it):

"

import os
import requests
import logging
from pydub import AudioSegment
from google.cloud import speech
import io
import main
import helpers
import llm
from HagaisPrompts import blog_prompts


# Configure logging
logging.basicConfig(level=logging.INFO)

def download_podcast(url, file_path="mypodcast.mp3"):
"""Download a podcast from a given URL and save it to a specified file path."""
try:
response = requests.get(url)
response.raise_for_status()
with open(file_path, 'wb') as file:
file.write(response.content)
logging.info("Podcast downloaded successfully.")
except requests.exceptions.RequestException as e:
logging.error(f"Error downloading podcast: {e}")

def transcribe_audio(file_path, language_code="en-US"):
"""Transcribe the given audio file using Google Speech-to-Text API."""
client = speech.SpeechClient()
with io.open(file_path, "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code=language_code,
)

try:
response = client.recognize(config=config, audio=audio)
return " ".join(result.alternatives[0].transcript for result in response.results)
except Exception as e:
logging.error(f"Error transcribing audio: {e}")
return ""

def split_audio(file_path, chunk_length_ms=60000):
"""Split audio into chunks of specified length."""
try:
audio = AudioSegment.from_file(file_path)
return [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
except Exception as e:
logging.error(f"Error splitting audio: {e}")
return []

def transcribe_segments(segments):
"""Transcribe each audio segment."""
transcriptions = []
for segment in segments:
transcriptions.append(transcribe_audio_segment(segment))
return " ".join(transcriptions)

def transcribe_audio_segment(segment):
"""Transcribe a single audio segment."""
client = speech.SpeechClient()
segment = segment.set_frame_rate(16000).set_sample_width(2).set_channels(1)
audio = speech.RecognitionAudio(content=segment.raw_data)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code="en-US",
)

try:
response = client.recognize(config=config, audio=audio)
return " ".join(result.alternatives[0].transcript for result in response.results)
except Exception as e:
logging.error(f"Error in transcription: {e}")
return ""

def podcast_to_summery_bullets(url_podcast):
"""Main function to download and transcribe a podcast."""
download_podcast(url_podcast)
segments = split_audio("mypodcast.mp3")
transcription = transcribe_segments(segments)

selected_module = "gpt-3.5-turbo-1106"
prompt = blog_prompts.blog_bullet_summary_prompt.format(
MaxPoints="10", MinPoints="5", InputText=transcription)
response = llm.llm_generate_text(prompt, "OpenAI", selected_module)
#print(transcription)
print(response)


if __name__ == "__main__":
podcast_to_summery_bullets("https://www.buzzsprout.com/442543/14100984-speak-better-english-with-harry-episode-461.mp3")

"

The problem is that I wanted to challenge myself and use whisper, and I was unable to reach a solution even with the help of ChatGPT. I entered an endless loop with him 🙂 here is the code:

"

import requests
import logging
from pydub import AudioSegment
import whisper
import main
import helpers
from HagaisPrompts import blog_prompts, fannel_prompts
import os

# Configure logging
logging.basicConfig(level=logging.INFO)

def download_podcast(url, file_path="mypodcast.mp3"):
"""Download a podcast from a given URL and save it to a specified file path."""
try:
response = requests.get(url)
response.raise_for_status()
with open(file_path, 'wb') as file:
file.write(response.content)
logging.info("Podcast downloaded successfully.")
return True
except requests.exceptions.RequestException as e:
logging.error(f"Error downloading podcast: {e}")
return False

def transcribe_audio(file_path, model="base"):
"""Transcribe the given audio file using OpenAI's Whisper API."""
try:
model = whisper.load_model(model)
result = model.transcribe(file_path)
return result["text"]
except Exception as e:
logging.error(f"Error transcribing audio with Whisper: {e}")
return None

def podcast_to_blog_post(url_podcast):
"""Main function to download, transcribe a podcast and convert it into a blog post."""
if not download_podcast(url_podcast):
return "Failed to download the podcast."

transcription = transcribe_audio("mypodcast.mp3")
if transcription is None:
return "Failed to transcribe the podcast."

# Remove the downloaded file to save space
os.remove("mypodcast.mp3")

# Convert transcription to blog post
selected_module = "gpt-3.5-turbo-1106"
blog_post_prompt = blog_prompts.blog_post_formatting_prompt.format(InputText=transcription)
blog_post = main.llm_generate_text(blog_post_prompt, "OpenAI", selected_module)

# Optimize blog post for SEO
seo_optimization_prompt = fannel_prompts.seo_keyword_generation.format(BlogPost=blog_post)
seo_optimized_blog_post = main.llm_generate_text(seo_optimization_prompt, "OpenAI", selected_module)

return seo_optimized_blog_post

if __name__ == "__main__":
result = podcast_to_blog_post("https://www.buzzsprout.com/442543/14100984-speak-better-english-with-harry-episode-461.mp3")
if result:
print(result)
else:
print("An error occurred during the process.")

"

i got the error:

Traceback (most recent call last):
File "D:\Users\hbenmoshe\promptEngineering\podcast2blogwhisper.py", line 4, in <module>
import whisper
File "D:\Users\hbenmoshe\promptEngineering\venv\Lib\site-packages\whisper.py", line 69, in <module>
libc = ctypes.CDLL(libc_name)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hbenm\AppData\Local\Programs\Python\Python311\Lib\ctypes\__init__.py", line 366, in __init__
if '/' in name or '\\' in name:
^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

and ChatGPT advise me to do that:

"

The error you're encountering is related to the whisper module in Python, specifically at the point where it's trying to load a shared library (libc). The error TypeError: argument of type 'NoneType' is not iterable suggests that the variable libc_name is None when it's expected to be a string representing the path to the shared library.

This issue could be due to several reasons:

  1. Environment Issue: The environment in which whisper is being run may not have the necessary dependencies installed or properly configured. This could be specific to how your Python environment is set up.

  2. Incorrect Installation of Dependencies: There might be an issue with how whisper or its dependencies are installed in your environment.

  3. Compatibility Issues: There could be compatibility issues with the Python version you are using (Python 3.11 as indicated in the error message) and the whisper module.

To resolve this issue, you can try the following steps:

  1. Check Python Version: Ensure that your Python version is compatible with whisper. You might need to use an earlier version of Python if whisper doesn't support Python 3.11 yet.

  2. Reinstall Whisper: Try reinstalling the whisper module. Sometimes, reinstalling can fix issues related to corrupted installations or missing dependencies.

    bash
    pip uninstall whisper
    pip install whisper
  3. Verify Dependencies: Make sure all dependencies required by whisper are installed and properly configured.

  4. Use Virtual Environment: If you're not already doing so, consider using a virtual environment. This can help in managing dependencies and their versions specific to a project without affecting the global Python setup.

  5. Check for Known Issues: Look at the GitHub issues page for whisper to see if others have encountered this problem and if any solutions or workarounds have been suggested.

  6. Update the Code: Ensure that your code is following the latest usage patterns and API calls as specified in the whisper documentation. Incompatibility between the code and the library version can sometimes lead to such errors.

If these steps do not resolve the issue, you may need to seek further assistance on forums or community platforms specific to Python or the whisper module, as the problem might be more specific to your development environment.

"

but it is not worked. please your assistance. thank you 

 
 
Posted : 12/22/2023 1:02 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

Thanks for sharing. Which GPT model are you using to create the code and ask about errors? I'll take a copy of the code and test it later.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/22/2023 3:25 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@agai-ben-moshe can you provide a public Github link for your code?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/22/2023 7:26 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Great job... really!

so what I understand, the problem is only about using whisper to get the text out of the audio yes? 

All other functions in the workflow are working as expected.

anyway, I just tried to use whisper, and it worked perfectly, here is the sample code I used:

import openai
openai.api_key = "sk-XXX"



def transcribe_audio(file_path, model_name):


    audio_file= open(file_path, "rb")
    transcript = openai.audio.transcriptions.create(
        model=model_name,
        file=audio_file
   
    )
    return transcript.text



# Usage example:
transcribed_text = transcribe_audio("test.wav", "whisper-1")
print(transcribed_text)

 

 
Posted : 12/23/2023 12:12 pm
SSAdvisor reacted
Share:
[the_ad_group id="312"]