Michtiii commited on
Commit
da21be1
·
verified ·
1 Parent(s): 7f6885d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 --------
10
+ def transcribe_audio(file_path):
11
+ with open(file_path, "rb") as audio:
12
+ transcript = client.audio.transcriptions.create(
13
+ model="whisper-1",
14
+ file=audio
15
+ )
16
+ return transcript.text
17
+
18
+ # -------- Extract CRM Fields --------
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 = eval(response.choices[0].message.content)
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", None, None
46
+
47
+ # Save temp file
48
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
49
+ temp_file.write(audio_file.read())
50
+ temp_file.close()
51
+
52
+ # Step 1: Transcribe
53
+ text = transcribe_audio(temp_file.name)
54
+
55
+ # Step 2: Extract fields
56
+ data = extract_fields(text)
57
+
58
+ # Convert to DataFrame
59
+ df = pd.DataFrame([data])
60
+
61
+ # Save Excel
62
+ excel_path = os.path.join(tempfile.gettempdir(), "crm_output.xlsx")
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(source="microphone", type="file")
74
+ btn1 = gr.Button("Process Recording")
75
+
76
+ with gr.Tab("📁 Upload Voice"):
77
+ file_input = gr.File(file_types=["audio"])
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
+ btn1.click(process_audio, inputs=mic_input, outputs=[transcript_output, table_output, download_btn])
85
+ btn2.click(process_audio, inputs=file_input, outputs=[transcript_output, table_output, download_btn])
86
+
87
+ app.launch()