Michtiii's picture
Update app.py
9e32d2f verified
import gradio as gr
import pandas as pd
import os
import tempfile
import re
from transformers import pipeline
# -------- Load Whisper Model (HF) --------
asr = pipeline(
"automatic-speech-recognition",
model="openai/whisper-small"
)
# -------- Speech to Text --------
def transcribe_audio(file_path):
result = asr(file_path)
return result["text"]
# -------- Simple NLP Extraction (Rule-based) --------
def extract_fields(text):
data = {
"Name": "",
"Phone": "",
"Product": "",
"Budget": "",
"Location": "",
"Intent": ""
}
text_lower = text.lower()
# Name extraction
match = re.search(r"my name is (\w+)", text_lower)
if match:
data["Name"] = match.group(1).title()
# Phone extraction
phone_match = re.search(r"\b\d{10}\b", text)
if phone_match:
data["Phone"] = phone_match.group()
# Budget extraction
if "lakh" in text_lower or "budget" in text_lower:
data["Budget"] = text
# Product (basic)
if "car" in text_lower:
data["Product"] = "Car"
elif "bike" in text_lower:
data["Product"] = "Bike"
else:
data["Product"] = text[:50]
# Location (basic guess)
if "in" in text_lower:
data["Location"] = text
# Intent
if "buy" in text_lower or "interested" in text_lower:
data["Intent"] = "Hot Lead"
else:
data["Intent"] = "Cold Lead"
return data
# -------- Main Processing --------
def process_audio(audio_file):
if audio_file is None:
return "No audio provided", pd.DataFrame(), None
try:
file_path = audio_file # Gradio gives filepath
# Step 1: Transcription
text = transcribe_audio(file_path)
# Step 2: Extraction
data = extract_fields(text)
# Step 3: DataFrame
df = pd.DataFrame([data])
# Step 4: Save Excel
excel_path = os.path.join(tempfile.gettempdir(), "crm_output.xlsx")
df.to_excel(excel_path, index=False)
return text, df, excel_path
except Exception as e:
return f"Error: {str(e)}", pd.DataFrame(), None
# -------- UI --------
with gr.Blocks() as app:
gr.Markdown("# πŸŽ™οΈ AI Voice to CRM Auto Filler ")
with gr.Tabs():
# 🎀 Record
with gr.Tab("🎀 Record Inquiry"):
mic_input = gr.Audio(
sources=["microphone"],
type="filepath",
label="Record Audio"
)
btn1 = gr.Button("Process Recording")
# πŸ“ Upload
with gr.Tab("πŸ“ Upload Voice"):
file_input = gr.Audio(
sources=["upload"],
type="filepath",
label="Upload Audio File"
)
btn2 = gr.Button("Process File")
# Outputs
transcript_output = gr.Textbox(label="Transcription")
table_output = gr.Dataframe(label="Extracted CRM Data")
download_btn = gr.File(label="Download Excel")
# Actions
btn1.click(
fn=process_audio,
inputs=mic_input,
outputs=[transcript_output, table_output, download_btn]
)
btn2.click(
fn=process_audio,
inputs=file_input,
outputs=[transcript_output, table_output, download_btn]
)
# Launch
app.launch()