Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import tempfile
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
from openai import OpenAI
|
| 6 |
|
|
|
|
| 7 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
|
| 9 |
# -------- Speech to Text --------
|
|
@@ -15,73 +17,118 @@ def transcribe_audio(file_path):
|
|
| 15 |
)
|
| 16 |
return transcript.text
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
def extract_fields(text):
|
| 20 |
prompt = f"""
|
| 21 |
Extract the following fields from the conversation:
|
|
|
|
| 22 |
Name, Phone, Product, Budget, Location, Intent.
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
Conversation:
|
| 25 |
{text}
|
| 26 |
-
|
| 27 |
-
Return in JSON format.
|
| 28 |
"""
|
| 29 |
|
| 30 |
response = client.chat.completions.create(
|
| 31 |
model="gpt-4o-mini",
|
| 32 |
-
messages=[{"role": "user", "content": prompt}]
|
|
|
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
data =
|
| 37 |
except:
|
| 38 |
-
data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
return data
|
| 41 |
|
|
|
|
| 42 |
# -------- Main Processing --------
|
| 43 |
def process_audio(audio_file):
|
| 44 |
if audio_file is None:
|
| 45 |
-
return "No audio provided",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
temp_file.write(audio_file.read())
|
| 50 |
-
temp_file.close()
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
df = pd.DataFrame([data])
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
df.to_excel(excel_path, index=False)
|
| 64 |
|
| 65 |
-
return text, df, excel_path
|
| 66 |
|
| 67 |
# -------- UI --------
|
| 68 |
with gr.Blocks() as app:
|
| 69 |
gr.Markdown("# ποΈ AI Voice to CRM Auto Filler")
|
| 70 |
|
| 71 |
with gr.Tabs():
|
|
|
|
| 72 |
with gr.Tab("π€ Record Inquiry"):
|
| 73 |
-
mic_input = gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
btn1 = gr.Button("Process Recording")
|
| 75 |
|
|
|
|
| 76 |
with gr.Tab("π Upload Voice"):
|
| 77 |
-
file_input = gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
btn2 = gr.Button("Process File")
|
| 79 |
|
|
|
|
| 80 |
transcript_output = gr.Textbox(label="Transcription")
|
| 81 |
table_output = gr.Dataframe(label="Extracted CRM Data")
|
| 82 |
download_btn = gr.File(label="Download Excel")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
| 87 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
+
import tempfile
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
+
# Initialize OpenAI client
|
| 9 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
|
| 11 |
# -------- Speech to Text --------
|
|
|
|
| 17 |
)
|
| 18 |
return transcript.text
|
| 19 |
|
| 20 |
+
|
| 21 |
+
# -------- Extract CRM Fields (SAFE JSON) --------
|
| 22 |
def extract_fields(text):
|
| 23 |
prompt = f"""
|
| 24 |
Extract the following fields from the conversation:
|
| 25 |
+
|
| 26 |
Name, Phone, Product, Budget, Location, Intent.
|
| 27 |
+
|
| 28 |
+
Return ONLY valid JSON like:
|
| 29 |
+
{{
|
| 30 |
+
"Name": "",
|
| 31 |
+
"Phone": "",
|
| 32 |
+
"Product": "",
|
| 33 |
+
"Budget": "",
|
| 34 |
+
"Location": "",
|
| 35 |
+
"Intent": ""
|
| 36 |
+
}}
|
| 37 |
+
|
| 38 |
Conversation:
|
| 39 |
{text}
|
|
|
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
response = client.chat.completions.create(
|
| 43 |
model="gpt-4o-mini",
|
| 44 |
+
messages=[{"role": "user", "content": prompt}],
|
| 45 |
+
temperature=0
|
| 46 |
)
|
| 47 |
|
| 48 |
+
content = response.choices[0].message.content
|
| 49 |
+
|
| 50 |
try:
|
| 51 |
+
data = json.loads(content)
|
| 52 |
except:
|
| 53 |
+
data = {
|
| 54 |
+
"Name": "",
|
| 55 |
+
"Phone": "",
|
| 56 |
+
"Product": "",
|
| 57 |
+
"Budget": "",
|
| 58 |
+
"Location": "",
|
| 59 |
+
"Intent": ""
|
| 60 |
+
}
|
| 61 |
|
| 62 |
return data
|
| 63 |
|
| 64 |
+
|
| 65 |
# -------- Main Processing --------
|
| 66 |
def process_audio(audio_file):
|
| 67 |
if audio_file is None:
|
| 68 |
+
return "No audio provided", pd.DataFrame(), None
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
file_path = audio_file # Gradio gives filepath directly
|
| 72 |
+
|
| 73 |
+
# Step 1: Transcription
|
| 74 |
+
text = transcribe_audio(file_path)
|
| 75 |
|
| 76 |
+
# Step 2: Extraction
|
| 77 |
+
data = extract_fields(text)
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# Step 3: Convert to DataFrame
|
| 80 |
+
df = pd.DataFrame([data])
|
| 81 |
|
| 82 |
+
# Step 4: Save Excel
|
| 83 |
+
excel_path = os.path.join(tempfile.gettempdir(), "crm_output.xlsx")
|
| 84 |
+
df.to_excel(excel_path, index=False)
|
| 85 |
|
| 86 |
+
return text, df, excel_path
|
|
|
|
| 87 |
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error: {str(e)}", pd.DataFrame(), None
|
|
|
|
| 90 |
|
|
|
|
| 91 |
|
| 92 |
# -------- UI --------
|
| 93 |
with gr.Blocks() as app:
|
| 94 |
gr.Markdown("# ποΈ AI Voice to CRM Auto Filler")
|
| 95 |
|
| 96 |
with gr.Tabs():
|
| 97 |
+
# π€ Record
|
| 98 |
with gr.Tab("π€ Record Inquiry"):
|
| 99 |
+
mic_input = gr.Audio(
|
| 100 |
+
sources=["microphone"],
|
| 101 |
+
type="filepath",
|
| 102 |
+
label="Record Audio"
|
| 103 |
+
)
|
| 104 |
btn1 = gr.Button("Process Recording")
|
| 105 |
|
| 106 |
+
# π Upload
|
| 107 |
with gr.Tab("π Upload Voice"):
|
| 108 |
+
file_input = gr.Audio(
|
| 109 |
+
sources=["upload"],
|
| 110 |
+
type="filepath",
|
| 111 |
+
label="Upload Audio File"
|
| 112 |
+
)
|
| 113 |
btn2 = gr.Button("Process File")
|
| 114 |
|
| 115 |
+
# Outputs
|
| 116 |
transcript_output = gr.Textbox(label="Transcription")
|
| 117 |
table_output = gr.Dataframe(label="Extracted CRM Data")
|
| 118 |
download_btn = gr.File(label="Download Excel")
|
| 119 |
|
| 120 |
+
# Actions
|
| 121 |
+
btn1.click(
|
| 122 |
+
fn=process_audio,
|
| 123 |
+
inputs=mic_input,
|
| 124 |
+
outputs=[transcript_output, table_output, download_btn]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
btn2.click(
|
| 128 |
+
fn=process_audio,
|
| 129 |
+
inputs=file_input,
|
| 130 |
+
outputs=[transcript_output, table_output, download_btn]
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
# Launch
|
| 134 |
app.launch()
|