M12faiez/Audio_Based_Stock_Analysis_Agent_PoC
0
1import pandas as pd2import gradio as gr3from transformers import pipeline4import whisper5 6 7# Load the TAPAS pipeline for table-question-answering8pipe = pipeline("table-question-answering", model="google/tapas-base-finetuned-wtq")9 10 11# Function to load the CSV file and prepare the table12def load_csv_table(csv_file):13 """14 Load the CSV file and convert it into a Pandas DataFrame.15 """16 df = pd.read_csv(csv_file.name) # Gradio provides the file as an object with a .name attribute17 table = df.astype(str) # Convert all values to strings for compatibility18 return table19 20 21# Function to use TAPAS pipeline to answer a question22def answer_question_with_pipeline(table, question):23 """24 Use TAPAS pipeline to answer the question based on the table.25 """26 result = pipe(table=table.to_dict(orient="records"), query=question)27 return result["answer"]28 29 30# Load the Whisper model for audio transcription31whisper_model = whisper.load_model("base")32 33def transcribe(audio):34 """35 Transcribe audio using the Whisper model.36 """37 # Load and preprocess audio38 audio = whisper.load_audio(audio)39 audio = whisper.pad_or_trim(audio)40 41 # Generate log-Mel spectrogram and transcribe42 mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)43 _, probs = whisper_model.detect_language(mel)44 print(f"Detected language: {max(probs, key=probs.get)}")45 46 # Decode the audio and return text47 options = whisper.DecodingOptions()48 result = whisper.decode(whisper_model, mel, options)49 return result.text50 51 52def process(csv_file, audio):53 """54 Process the uploaded CSV file and audio to generate an answer.55 """56 # Step 1: Transcribe the audio to get the question57 question = transcribe(audio)58 59 # Step 2: Load the table from the uploaded CSV file60 table = load_csv_table(csv_file)61 62 # Step 3: Use TAPAS to answer the transcribed question63 answer = answer_question_with_pipeline(table, question)64 65 return question, answer66 67 68iface = gr.Interface(69 fn=process, # Main function integrating Whisper and TAPAS70 inputs=[71 gr.File(label="Upload CSV File and ask quuestion i.e which company_name has most volume value"), # Upload CSV file72 gr.Audio(type="filepath", label="Record or Upload Audio for Question (wait for audio to be processed if getting error, click submit again)") # Record/upload audio73 ],74 outputs=[75 gr.Textbox(label="Transcribed Question"), # Display transcribed question76 gr.Textbox(label="Answer from Table") # Display TAPAS answer77 ],78 title="Table Question Answering with Audio Input",79 description="Upload a CSV file, ask your question via audio, and get answers using TAPAS."80)81 82# Launch Gradio Interface83iface.launch()84 