Michtiii's picture
Update app.py
65a7426 verified
import gradio as gr
import pandas as pd
import os
import json
import tempfile
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# -------- Speech to Text --------
def transcribe_audio(file_path):
with open(file_path, "rb") as audio:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio
)
return transcript.text
# -------- Extract CRM Fields (SAFE JSON) --------
def extract_fields(text):
prompt = f"""
Extract the following fields from the conversation:
Name, Phone, Product, Budget, Location, Intent.
Return ONLY valid JSON like:
{{
"Name": "",
"Phone": "",
"Product": "",
"Budget": "",
"Location": "",
"Intent": ""
}}
Conversation:
{text}
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
content = response.choices[0].message.content
try:
data = json.loads(content)
except:
data = {
"Name": "",
"Phone": "",
"Product": "",
"Budget": "",
"Location": "",
"Intent": ""
}
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 directly
# Step 1: Transcription
text = transcribe_audio(file_path)
# Step 2: Extraction
data = extract_fields(text)
# Step 3: Convert to 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()