File size: 9,121 Bytes
2d8bbf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from openai import OpenAI as OPENAI
import os
import gradio as gr
import json

client = OPENAI()
client.api_key = os.environ.get("OPENAI_API_KEY")


conversation_history=[]

def predict(input):

    completion = client.completion.create(
    model="gpt-4", 
    messages=[{"role": "user", "content": input}]
    )
    response = completion.choices[0].messag.content

    return response



def initial_prompt():

    # client.api_key = os.environ.get("OPENAI_API_KEY")

    f = open('templates/record_types.json')
    crm_temlate_json = json.load(f)

    global conversation_history
    default_message = (f"""
    Microsoft Dynamic CRM için kayıt olusturmada yardımcı bir asistan gibi davranmanı istiyorum.
    Senin isin yalnizca CRM kayit olsuturma ve yonetme konusunda yardimci olmak, bu sebeple kullanici
    CRM haricinde baska bir konuda konusmak isterse onca kibarca gorevini hatirlatarak istedigi islemi 
    yapamayacagini belirtiyorsun. Kullanici seninle iletisime gectiginde kendini CRM Asistan Bot olarak 
    tanitarak nasil yardimci olabilecegini soracaksin.
    Kullanicinin yapmak istedigi isleme gore {crm_temlate_json} json verisi icinden 'Contact' yada 'Aktivite' 
    templatelerine gore kullanicin girmesi gereken input verilerini isteyeceksin.
    Kullanıcı seninle iletişime geçtiğinde amaçlarını ögrenip, amaclanan kayit icin ihtiyac olabilecek 
    alan bilgilerini sirasiyla, adim adim kullanicidan isteyecek ve islemler bittiginda bu bilgileri 
    json olarak doneceksin. Kullanici kayit olusturman icin onaylamadigi surece
    ek bilgi ekleyip eklemek istemedigini soracaksin. Ne olursa olsun kullanicinin belirtmesi gereken bilgileri kendin girmeyeceksin.
    Kullanici ek bilgi eklemek isterse sirasiyla o bilgileri isteyerek olusturacagin kayit bilgisine ekleyeceksin.
    Ornek: [Kullanici:"Merhaba", Asistan:"Merhaba! Size nasil yardimci olabilirim?", 
    Kullanici: "Kontakt kaydi olusturmak istiyorum", ...]. Ilk kayit ile baslayalim
    """)

    conversation_history.append({"role": "system", "content": f"{default_message}"})

    completion = client.chat.completions.create(
        model="gpt-4", 
        messages=[{"role": "system", "content": " ".join([str(item) for item in conversation_history])}],
        temperature=0.3,
        max_tokens=7500,
    )

    message = completion.choices[0].message.content
    print(message)

initial_prompt()

def handle_input(input_str : str):
    global conversation_history

    if len(conversation_history) >=20:
        conversation_history = conversation_history[:1] + conversation_history[-10:]

    conversation_history.append({"role": "user", "content": f"{input_str}"})
    
    content = " ".join([str(item) for item in conversation_history])
    completion = client.chat.completions.create(
        model="gpt-4", 
        messages=conversation_history,
        # temperature=temperature,
        # max_tokens=max_tokens,
        # presence_penalty=presence_penalty,
        # frequency_penalty=frequency_penalty,
        # top_p = top_p_input,
        #stream = stream_input
    )

    
    
    message = completion.choices[0].message.content

    conversation_history.append({"role": "assistant", "content": f"{message}"})

    def get_response_again(content): 

        while True:
            completion = client.chat.completion.create(
                model="gpt-4", 
                messages=[{"role": "assistant", "content": content}],
                # temperature=temperature,
                # max_tokens=max_tokens,
                # presence_penalty=presence_penalty,
                # frequency_penalty=frequency_penalty,
                # top_p = top_p_input,
                #stream = stream_input
            )

            message = completion.choices[0].message.content

            if "Müşteri" not in message:
                break

        return message

    # if "Müşteri" in message:
    #     get_response_again(content)

    # conversation_history.append(f"{message}\n")
    
    return message

def get_model_reply(query,context=[]):
 
    context += [query]
    
    # client.api_key = api_key

    response = handle_input(query)

    
    context += [response]
    
    responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
    
    return responses, context


#TODO Thiws feature will be added later
def speech_2_text(audio,api_key, context=[]):
    client.api_key = api_key
    audio_file= open(audio, "rb")
    transcript = client.Audio.transcribe("whisper-1", audio_file)
    prompt = transcript.text
    context += [prompt]

    completion = client.chat.completion.create(
        model="gpt-4", 
        messages=[{"role": "user", "content": prompt}],
        max_tokens=2500,
    )

    response = completion.choices[0].message.content
    context += [response]
    
    responses = [(u,b) for u,b in zip(context[::2], context[1::2])]

    return responses, context

# def get_audio(url,api_key,context=[]):
    client.api_key = api_key
    destination = f"./audio/test"
    try:
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl':destination,
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        
        audio_file= open(f'{destination}.mp3', "rb")
        transcript = client.Audio.transcribe("whisper-1", audio_file)

        context += [transcript.text]
        
        responses = [(u,b) for u,b in zip(context[::2], context[1::2])]

        return responses, context
    
    except Exception as e:
        print("Connection Error")


with gr.Blocks(css="#chatbot {overflow-y:auto; height:400px;}") as dialog_app:

    with gr.Tab("ChatBot"):
        with gr.Row():
            # with gr.Column(scale=1, min_width=600):
                # api_key = gr.Textbox(label="Your API Key", type="password")
                # temperature_input = gr.Slider(minimum=0, maximum=1.0, default=0.5, step=0.01, label="Temperature")
                # max_tokens_input = gr.inputs.Slider(minimum=1, maximum=2048, default=50, step=10, label="Max Tokens")
                # top_p_input = gr.inputs.Slider(minimum=0.1, maximum=1.0, default=0.5, step=0.01, label="Top P")
                # presence_penalty_input = gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.0, step=0.1, label="Presence Penalty")
                # frequency_penalty_input = gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.0, step=0.1, label="Frequency Penalty")
                #stream_input = gr.inputs.Checkbox(label="Stream")

            with gr.Column(scale=2, min_width=600):
                chatbot = gr.Chatbot(elem_id="chatbot")
                state = gr.State([])

                txt = gr.Textbox(
                    show_label=False, 
                    placeholder="Enter text and press enter",
                    container=False
                )

        # txt.submit(get_model_reply, [txt, api_key, temperature_input, max_tokens_input,top_p_input, presence_penalty_input,frequency_penalty_input, state], [chatbot, state])
        txt.submit(get_model_reply, [txt, state], [chatbot, state])
        txt.submit(lambda :"", None, txt)

    
    # with gr.Tab("Voice Chat"):
    #     with gr.Row():
    #         with gr.Column(scale=1, min_width=600):
    #             voice_api_key = gr.Textbox(label="Your API Key", type="password")
    #             voice_state = gr.State([])
    #             youtube_url = gr.Textbox(
    #                 show_label=False, 
    #                 type="text", 
    #                 placeholder="Enter an Youtube URL")

    #             mic_audio = gr.Audio(source="microphone", type="filepath")

    #             with gr.Row():
    #                 with gr.Column(scale=1, min_width=250):
    #                     audio_clean_btn = gr.Button(value='Clean')
    #                 with gr.Column(scale=2, min_width=250):
    #                     audio_sbmt_btn = gr.Button(value='Submit', variant='primary')

    #         with gr.Column(scale=2, min_width=600):
    #             voice_bot = gr.Chatbot(elem_id="chatbot")
    #             #txt_box = gr.Textbox(type="text")
    #             voice_state = gr.State([])

    #             txt = gr.Textbox(
    #                 show_label=False, 
    #                 placeholder="Enter text and press enter"
    #             ).style(container=False)
            
    #         #youtube_url.submit(get_audio, [youtube_url,voice_api_key, state], [voice_bot, state])
    #         #audio_clean_btn.click(clean_audio, )
    #         audio_sbmt_btn.click(speech_2_text, inputs=[mic_audio,voice_api_key,voice_state], outputs=[voice_bot, voice_state])

    #         txt.submit(get_model_reply, [txt, voice_api_key, temperature_input, max_tokens_input,top_p_input, presence_penalty_input,frequency_penalty_input, voice_state], [voice_bot, voice_state])
    #         txt.submit(lambda :"", None, txt)
    

dialog_app.launch()