Query Audio with Gemini 1.5

Query Audio with Gemini 1.5

Implementation

This Streamlit-based script outlines an online software that leverages Google’s generative AI to course of audio information based on user-provided prompts. The software begins by configuring the required API keys and organising the surroundings. It introduces a perform, ‘process_audio’, which invokes a mannequin from Google’s Generative AI to interpret the uploaded audio file within the context of the person’s immediate. The immediate may very well be a request for summarization, sentiment evaluation, or different audio analyses.

A utility perform, ‘save_uploaded_file’, manages the non permanent storage of the uploaded audio, making certain it’s accessible for processing. The Streamlit app interface offers a user-friendly surroundings with a title and a sidebar that features the developer’s bio and a LinkedIn profile for skilled connection. Custom CSS is injected immediately into the app to boost the visible parts, like buttons and textual content inputs, making a extra partaking person expertise.

Users work together with the app by a textual content enter discipline, the place they will enter customized prompts that instruct the AI on what to do with the uploaded audio file. The audio file might be uploaded by way of a file uploader that accepts WAV and MP3 codecs. Once uploaded, the file might be performed again immediately within the app, and with the press of a button, the audio is processed based on the person’s directions. The outcomes are displayed in a textual content space that may be scrolled by, permitting customers to learn by the processed output generated by the AI.

This script represents the backend of an online software designed to make AI-powered audio evaluation accessible and simple to make use of for a variety of purposes.

import streamlit as st
import tempfile
import os
import google.generativeai as genai

from dotenv import load_dotenv

load_dotenv()

# Configure Google API for audio processing
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)

def process_audio(audio_file_path, user_prompt):
"""Process the audio utilizing the person's immediate with Google's Generative API."""
mannequin = genai.GenerativeMannequin("fashions/gemini-1.5-pro-latest")
audio_file = genai.upload_file(path=audio_file_path)
response = mannequin.generate_content(
[
user_prompt,
audio_file
]
)
return response.textual content

def save_uploaded_file(uploaded_file):
"""Save uploaded file to a short lived file and return the trail."""
attempt:
with tempfile.NamedTemporaryFile(delete=False, suffix='.' + uploaded_file.title.break up('.')[-1]) as tmp_file:
tmp_file.write(uploaded_file.getvalue())
return tmp_file.title
besides Exception as e:
st.error(f"Error dealing with uploaded file: {e}")
return None

# Streamlit app interface
st.title('AI-Powered Audio Processing App')

# Profile Sidebar
st.sidebar.title('About Me')
st.sidebar.picture('TOni ANotherpic.jpg', width=100) # Replace URL
st.sidebar.markdown("""
**Name:** Toni Ramchandani
**Bio:** Driven by Sports, Adventure, Technology & Innovations.
[LinkedIn Profile](https://www.linkedin.com/in/toni-ramchandani/)
""")

# Inject customized CSS for styling
st.markdown("""
<fashion>
.stButton>button {
width: 150px;
top: 50px;
border-radius: 5px;
border: 2px strong #FF4B4B;
background-color: #FF4B4B;
coloration: white;
font-size: 16px;
font-weight: daring;
margin: 10px;
transition: background-color 0.3s, coloration 0.3s;
}
.stButton>button:hover {
background-color: #FF0000;
coloration: yellow;
}
.stTextInput>div>div>enter {
font-size: 16px;
top: 40px;
width: 100%;
border-radius: 5px;
}
.stTextArea>div>div>textarea {
font-size: 16px;
top: 200px;
width: 100%;
border-radius: 5px;
}
</fashion>
""", unsafe_allow_html=True)

user_prompt = st.text_input("Enter your customized AI immediate:", placeholder="E.g., 'Please summarize the audio:'")

audio_file = st.file_uploader("Upload Audio File", sort=['wav', 'mp3'])
if audio_file isn't None:
audio_path = save_uploaded_file(audio_file)
st.audio(audio_path)

if st.button('Process Audio'):
with st.spinner('Processing...'):
processed_text = process_audio(audio_path, user_prompt)
st.text_area("Processed Output", processed_text, top=300)

Find all of the code on the above Github repo.

HI-FI News

by way of AI on Medium https://ift.tt/9xaEGor

April 15, 2024 at 04:03PM