rakeshrohan commited on
Commit
1975a0d
·
verified ·
1 Parent(s): cc9c99c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import base64
5
+ import openai
6
+
7
+ LEPTON_API_TOKEN = "o36eVFfCrItZ9hcImxjzeCxk0jPfc7fg"
8
+
9
+ client = openai.OpenAI(
10
+ base_url="https://llama3-2-3b.lepton.run/api/v1/",
11
+ api_key=os.environ.get('LEPTON_API_TOKEN', LEPTON_API_TOKEN)
12
+ )
13
+
14
+ SYSTEM_PROMPT = """
15
+ You are an AI assistant designed to conduct conversations related to collecting outstanding balances or resolving account issues. Your goal is to professionally address the situation while being empathetic and solution-oriented. Maintain a friendly yet professional tone throughout the interaction.
16
+ """
17
+
18
+ def transcribe_audio(audio):
19
+ if audio is None:
20
+ return "No audio detected. Please try again."
21
+
22
+ with open(audio, "rb") as f:
23
+ audio_bytes = f.read()
24
+ audio_data = base64.b64encode(audio_bytes).decode()
25
+
26
+ try:
27
+ completion = client.chat.completions.create(
28
+ model="gpt-3.5-turbo",
29
+ extra_body={
30
+ "tts_audio_format": "opus",
31
+ "tts_audio_bitrate": 16,
32
+ "require_audio": True,
33
+ "tts_preset_id": "jessica",
34
+ },
35
+ messages=[
36
+ {"role": "user", "content": [{"type": "audio", "data": audio_data}]}
37
+ ],
38
+ max_tokens=128,
39
+ stream=True,
40
+ )
41
+
42
+ text_response = ""
43
+ for chunk in completion:
44
+ if not chunk.choices:
45
+ continue
46
+ content = chunk.choices[0].delta.content
47
+ if content:
48
+ text_response += content
49
+
50
+ return text_response
51
+ except Exception as e:
52
+ return f"Error in transcribing audio: {str(e)}"
53
+
54
+ def ai_response(user_message, chat_history):
55
+ try:
56
+ context = [{"role": "system", "content": SYSTEM_PROMPT}]
57
+ for human, ai in chat_history:
58
+ context.append({"role": "user", "content": human})
59
+ context.append({"role": "assistant", "content": ai})
60
+ context.append({"role": "user", "content": user_message})
61
+
62
+ completion = client.chat.completions.create(
63
+ model="gpt-3.5-turbo",
64
+ extra_body={
65
+ "tts_audio_format": "opus",
66
+ "tts_audio_bitrate": 16,
67
+ "require_audio": True,
68
+ "tts_preset_id": "jessica",
69
+ },
70
+ messages=context,
71
+ max_tokens=256,
72
+ stream=True,
73
+ )
74
+
75
+ ai_message = ""
76
+ audio_response = []
77
+
78
+ for chunk in completion:
79
+ if not chunk.choices:
80
+ continue
81
+ content = chunk.choices[0].delta.content
82
+ audio = getattr(chunk.choices[0], 'audio', [])
83
+ if content:
84
+ ai_message += content
85
+ if audio:
86
+ audio_response.extend(audio)
87
+
88
+ audio_output = b''.join([base64.b64decode(audio) for audio in audio_response])
89
+
90
+ with open('ai_response.opus', 'wb') as f:
91
+ f.write(audio_output)
92
+
93
+ chat_history.append((user_message, ai_message))
94
+ return chat_history, 'ai_response.opus'
95
+ except Exception as e:
96
+ error_message = f"Error in generating AI response: {str(e)}"
97
+ chat_history.append((user_message, error_message))
98
+ return chat_history, None
99
+
100
+ def process_audio(audio, chat_history):
101
+ user_message = transcribe_audio(audio)
102
+ chat_history, ai_audio = ai_response(user_message, chat_history)
103
+ return chat_history, ai_audio, chat_history
104
+
105
+ iface = gr.Interface(
106
+ fn=process_audio,
107
+ inputs=[
108
+ gr.Audio(sources=["microphone", "upload"], type="filepath"),
109
+ gr.State([])
110
+ ],
111
+ outputs=[
112
+ gr.Chatbot(),
113
+ gr.Audio(type="filepath"),
114
+ gr.State()
115
+ ],
116
+ title="AI Assistant for Collections",
117
+ description="Have a conversation with an AI assistant to resolve account issues or outstanding balances. You can speak through your microphone or upload an audio file.",
118
+ allow_flagging="never"
119
+ )
120
+
121
+ iface.launch()