Michtiii commited on
Commit
65a7426
Β·
verified Β·
1 Parent(s): a3c941f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -27
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
- # -------- 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()
 
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()