Spaces:
Sleeping
Sleeping
Vlad Bastina commited on
Commit ·
68b5237
1
Parent(s): da622d4
code documentation
Browse files- gemini_call.py +3 -1
- sentiment_analysis.py +1 -1
- streamlit_app.py +2 -0
- translation.py +5 -0
gemini_call.py
CHANGED
|
@@ -69,6 +69,7 @@ genai.configure(api_key=api_key)
|
|
| 69 |
model = genai.GenerativeModel("gemini-2.0-flash-exp" , system_instruction=final_prompt)
|
| 70 |
|
| 71 |
def extract_histogram_data(text):
|
|
|
|
| 72 |
# Regex to match the sentences and their sentiment score
|
| 73 |
histogram_pattern = r"Sentence (\d+): (-?\+?\d+)"
|
| 74 |
|
|
@@ -82,6 +83,7 @@ def extract_histogram_data(text):
|
|
| 82 |
return sentences, sentiment_scores
|
| 83 |
|
| 84 |
def plot_histogram(sentences, sentiment_scores):
|
|
|
|
| 85 |
plt.figure(figsize=(6, 4))
|
| 86 |
plt.bar(sentences, sentiment_scores, color='skyblue')
|
| 87 |
plt.xlabel('Sentence Number')
|
|
@@ -93,7 +95,7 @@ def plot_histogram(sentences, sentiment_scores):
|
|
| 93 |
plt.close()
|
| 94 |
|
| 95 |
def ask_gemini(prompt:str) ->str:
|
| 96 |
-
|
| 97 |
|
| 98 |
response = model.generate_content(prompt)
|
| 99 |
|
|
|
|
| 69 |
model = genai.GenerativeModel("gemini-2.0-flash-exp" , system_instruction=final_prompt)
|
| 70 |
|
| 71 |
def extract_histogram_data(text):
|
| 72 |
+
"""Searches for reges to extract the info required for building the histogram"""
|
| 73 |
# Regex to match the sentences and their sentiment score
|
| 74 |
histogram_pattern = r"Sentence (\d+): (-?\+?\d+)"
|
| 75 |
|
|
|
|
| 83 |
return sentences, sentiment_scores
|
| 84 |
|
| 85 |
def plot_histogram(sentences, sentiment_scores):
|
| 86 |
+
"""Plots the histogram"""
|
| 87 |
plt.figure(figsize=(6, 4))
|
| 88 |
plt.bar(sentences, sentiment_scores, color='skyblue')
|
| 89 |
plt.xlabel('Sentence Number')
|
|
|
|
| 95 |
plt.close()
|
| 96 |
|
| 97 |
def ask_gemini(prompt:str) ->str:
|
| 98 |
+
"""Performs the api call to gemini and returns the answear and makes the plot of the histogram"""
|
| 99 |
|
| 100 |
response = model.generate_content(prompt)
|
| 101 |
|
sentiment_analysis.py
CHANGED
|
@@ -5,7 +5,7 @@ from dotenv import load_dotenv
|
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
def get_analysis(file_path)->str:
|
| 8 |
-
|
| 9 |
transcript = get_transcription_from_sound(file_path)
|
| 10 |
|
| 11 |
analysis = ask_gemini(transcript)
|
|
|
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
def get_analysis(file_path)->str:
|
| 8 |
+
"""Makes the pipeline to perform the sentiment analysis on a .wav file"""
|
| 9 |
transcript = get_transcription_from_sound(file_path)
|
| 10 |
|
| 11 |
analysis = ask_gemini(transcript)
|
streamlit_app.py
CHANGED
|
@@ -16,6 +16,7 @@ def save_audio(audio_data, filename):
|
|
| 16 |
wf.writeframes(audio_data) # Write audio data to file
|
| 17 |
|
| 18 |
def display_message(role, content, image_path=None):
|
|
|
|
| 19 |
if role == "user":
|
| 20 |
st.chat_message(role).markdown(f"**User:** {content}")
|
| 21 |
elif role == "gemini":
|
|
@@ -29,6 +30,7 @@ def display_message(role, content, image_path=None):
|
|
| 29 |
st.error(f"Error loading image: {e}")
|
| 30 |
|
| 31 |
def main():
|
|
|
|
| 32 |
st.title("Streamlit Voice Recorder")
|
| 33 |
|
| 34 |
if "messages" not in st.session_state:
|
|
|
|
| 16 |
wf.writeframes(audio_data) # Write audio data to file
|
| 17 |
|
| 18 |
def display_message(role, content, image_path=None):
|
| 19 |
+
"""Displays the messages on the screen along with the plots"""
|
| 20 |
if role == "user":
|
| 21 |
st.chat_message(role).markdown(f"**User:** {content}")
|
| 22 |
elif role == "gemini":
|
|
|
|
| 30 |
st.error(f"Error loading image: {e}")
|
| 31 |
|
| 32 |
def main():
|
| 33 |
+
"""Main function that loops and can record audio and dispaly the messages from gemini"""
|
| 34 |
st.title("Streamlit Voice Recorder")
|
| 35 |
|
| 36 |
if "messages" not in st.session_state:
|
translation.py
CHANGED
|
@@ -12,11 +12,15 @@ def get_audio_properties(file_path):
|
|
| 12 |
return sample_rate, channels
|
| 13 |
|
| 14 |
def convert_to_mono(input_path, output_path):
|
|
|
|
|
|
|
| 15 |
audio = AudioSegment.from_wav(input_path)
|
| 16 |
mono_audio = audio.set_channels(1)
|
| 17 |
mono_audio.export(output_path, format="wav")
|
| 18 |
|
| 19 |
def transcribe_audio(file_path):
|
|
|
|
|
|
|
| 20 |
# Initialize the speech client
|
| 21 |
client = speech.SpeechClient()
|
| 22 |
|
|
@@ -47,6 +51,7 @@ def transcribe_audio(file_path):
|
|
| 47 |
return concatenated_transcript
|
| 48 |
|
| 49 |
def get_transcription_from_sound(file_path:str)->str:
|
|
|
|
| 50 |
output_path = "audio_mono.wav"
|
| 51 |
convert_to_mono(file_path,output_path)
|
| 52 |
|
|
|
|
| 12 |
return sample_rate, channels
|
| 13 |
|
| 14 |
def convert_to_mono(input_path, output_path):
|
| 15 |
+
"""Convert video from 2+ channel audio to 1 channel audio for a
|
| 16 |
+
single detection"""
|
| 17 |
audio = AudioSegment.from_wav(input_path)
|
| 18 |
mono_audio = audio.set_channels(1)
|
| 19 |
mono_audio.export(output_path, format="wav")
|
| 20 |
|
| 21 |
def transcribe_audio(file_path):
|
| 22 |
+
"""Gets the .wav file path and perform speech to text
|
| 23 |
+
returns the string witch represents the words spoken"""
|
| 24 |
# Initialize the speech client
|
| 25 |
client = speech.SpeechClient()
|
| 26 |
|
|
|
|
| 51 |
return concatenated_transcript
|
| 52 |
|
| 53 |
def get_transcription_from_sound(file_path:str)->str:
|
| 54 |
+
"""Converts the audio to a single channel and calls the transcription function"""
|
| 55 |
output_path = "audio_mono.wav"
|
| 56 |
convert_to_mono(file_path,output_path)
|
| 57 |
|