Upload 4 files
Browse files
README.md
CHANGED
@@ -1,12 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 🐢
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.12.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# CrmBot
|
2 |
+
Crm Functionality Helper Bot Asistant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, render_template
|
2 |
+
from openai import OpenAI as OPENAI
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
import json
|
6 |
+
|
7 |
+
client = OPENAI()
|
8 |
+
client.api_key = os.environ.get("OPENAI_API_KEY")
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
conversation_history=[]
|
13 |
+
|
14 |
+
def handle_input(input_str : str):
|
15 |
+
global conversation_history
|
16 |
+
|
17 |
+
if len(conversation_history) >=20:
|
18 |
+
conversation_history = conversation_history[:1] + conversation_history[-10:]
|
19 |
+
|
20 |
+
conversation_history.append({"role": "user", "content": f"{input_str}"})
|
21 |
+
|
22 |
+
content = " ".join([str(item) for item in conversation_history])
|
23 |
+
completion = client.chat.completions.create(
|
24 |
+
model="gpt-4",
|
25 |
+
messages=conversation_history,
|
26 |
+
# temperature=temperature,
|
27 |
+
# max_tokens=max_tokens,
|
28 |
+
# presence_penalty=presence_penalty,
|
29 |
+
# frequency_penalty=frequency_penalty,
|
30 |
+
# top_p = top_p_input,
|
31 |
+
#stream = stream_input
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
message = completion.choices[0].message.content
|
37 |
+
|
38 |
+
conversation_history.append({"role": "assistant", "content": f"{message}"})
|
39 |
+
|
40 |
+
return message
|
41 |
+
|
42 |
+
def initialize():
|
43 |
+
f = open('templates/record_types.json')
|
44 |
+
crm_temlate_json = json.load(f)
|
45 |
+
|
46 |
+
global conversation_history
|
47 |
+
default_message = (f"""
|
48 |
+
Microsoft Dynamic CRM için kayıt olusturmada yardımcı bir asistan gibi davranmanı istiyorum.
|
49 |
+
Senin isin yalnizca CRM kayit olsuturma ve yonetme konusunda yardimci olmak, bu sebeple kullanici
|
50 |
+
CRM haricinde baska bir konuda konusmak isterse onca kibarca gorevini hatirlatarak istedigi islemi
|
51 |
+
yapamayacagini belirtiyorsun. Kullanici seninle iletisime gectiginde kendini CRM Asistan Bot olarak
|
52 |
+
tanitarak nasil yardimci olabilecegini soracaksin.
|
53 |
+
Kullanicinin yapmak istedigi isleme gore {crm_temlate_json} json verisi icinden 'Contact' yada 'Aktivite'
|
54 |
+
templatelerine gore kullanicin girmesi gereken input verilerini isteyeceksin.
|
55 |
+
Kullanıcı seninle iletişime geçtiğinde amaçlarını ögrenip, amaclanan kayit icin ihtiyac olabilecek
|
56 |
+
alan bilgilerini sirasiyla, adim adim kullanicidan isteyecek ve islemler bittiginda bu bilgileri
|
57 |
+
json olarak doneceksin. Kullanici kayit olusturman icin onaylamadigi surece
|
58 |
+
ek bilgi ekleyip eklemek istemedigini soracaksin. Ne olursa olsun kullanicinin belirtmesi gereken bilgileri kendin girmeyeceksin.
|
59 |
+
Kullanici ek bilgi eklemek isterse sirasiyla o bilgileri isteyerek olusturacagin kayit bilgisine ekleyeceksin.
|
60 |
+
Ornek: [Kullanici:"Merhaba", Asistan:"Merhaba! Size nasil yardimci olabilirim?",
|
61 |
+
Kullanici: "Kontakt kaydi olusturmak istiyorum", ...]. Ilk kayit ile baslayalim
|
62 |
+
""")
|
63 |
+
|
64 |
+
conversation_history.append({"role": "system", "content": f"{default_message}"})
|
65 |
+
|
66 |
+
completion = client.chat.completions.create(
|
67 |
+
model="gpt-4",
|
68 |
+
messages=[{"role": "system", "content": " ".join([str(item) for item in conversation_history])}],
|
69 |
+
temperature=0.3,
|
70 |
+
max_tokens=7500,
|
71 |
+
)
|
72 |
+
|
73 |
+
message = completion.choices[0].message.content
|
74 |
+
print(message)
|
75 |
+
|
76 |
+
@app.route('/')
|
77 |
+
def my_form():
|
78 |
+
return render_template('my-form.html')
|
79 |
+
|
80 |
+
@app.route('/send_message', methods=['POST'])
|
81 |
+
def send_message():
|
82 |
+
text = request.form['text']
|
83 |
+
answer = handle_input(text)
|
84 |
+
return answer
|
85 |
+
|
86 |
+
|
87 |
+
if __name__ == '__main__':
|
88 |
+
initialize()
|
89 |
+
app.run()
|
client
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
def send_message_to_server(text):
|
4 |
+
url = 'http://localhost:5000/send_message' # Flask servisinizin URL'si
|
5 |
+
data = {'text': text}
|
6 |
+
response = requests.post(url, data=data)
|
7 |
+
return response.text
|
8 |
+
|
9 |
+
if __name__ == '__main__':
|
10 |
+
while True:
|
11 |
+
user_input = input("Mesajınızı girin (çıkmak için 'exit' yazın): ")
|
12 |
+
if user_input.lower() == 'exit':
|
13 |
+
break
|
14 |
+
server_response = send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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI as OPENAI
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import json
|
5 |
+
|
6 |
+
client = OPENAI()
|
7 |
+
client.api_key = os.environ.get("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
|
10 |
+
conversation_history=[]
|
11 |
+
|
12 |
+
def predict(input):
|
13 |
+
|
14 |
+
completion = client.completion.create(
|
15 |
+
model="gpt-4",
|
16 |
+
messages=[{"role": "user", "content": input}]
|
17 |
+
)
|
18 |
+
response = completion.choices[0].messag.content
|
19 |
+
|
20 |
+
return response
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
def initial_prompt():
|
25 |
+
|
26 |
+
# client.api_key = os.environ.get("OPENAI_API_KEY")
|
27 |
+
|
28 |
+
f = open('templates/record_types.json')
|
29 |
+
crm_temlate_json = json.load(f)
|
30 |
+
|
31 |
+
global conversation_history
|
32 |
+
default_message = (f"""
|
33 |
+
Microsoft Dynamic CRM için kayıt olusturmada yardımcı bir asistan gibi davranmanı istiyorum.
|
34 |
+
Senin isin yalnizca CRM kayit olsuturma ve yonetme konusunda yardimci olmak, bu sebeple kullanici
|
35 |
+
CRM haricinde baska bir konuda konusmak isterse onca kibarca gorevini hatirlatarak istedigi islemi
|
36 |
+
yapamayacagini belirtiyorsun. Kullanici seninle iletisime gectiginde kendini CRM Asistan Bot olarak
|
37 |
+
tanitarak nasil yardimci olabilecegini soracaksin.
|
38 |
+
Kullanicinin yapmak istedigi isleme gore {crm_temlate_json} json verisi icinden 'Contact' yada 'Aktivite'
|
39 |
+
templatelerine gore kullanicin girmesi gereken input verilerini isteyeceksin.
|
40 |
+
Kullanıcı seninle iletişime geçtiğinde amaçlarını ögrenip, amaclanan kayit icin ihtiyac olabilecek
|
41 |
+
alan bilgilerini sirasiyla, adim adim kullanicidan isteyecek ve islemler bittiginda bu bilgileri
|
42 |
+
json olarak doneceksin. Kullanici kayit olusturman icin onaylamadigi surece
|
43 |
+
ek bilgi ekleyip eklemek istemedigini soracaksin. Ne olursa olsun kullanicinin belirtmesi gereken bilgileri kendin girmeyeceksin.
|
44 |
+
Kullanici ek bilgi eklemek isterse sirasiyla o bilgileri isteyerek olusturacagin kayit bilgisine ekleyeceksin.
|
45 |
+
Ornek: [Kullanici:"Merhaba", Asistan:"Merhaba! Size nasil yardimci olabilirim?",
|
46 |
+
Kullanici: "Kontakt kaydi olusturmak istiyorum", ...]. Ilk kayit ile baslayalim
|
47 |
+
""")
|
48 |
+
|
49 |
+
conversation_history.append({"role": "system", "content": f"{default_message}"})
|
50 |
+
|
51 |
+
completion = client.chat.completions.create(
|
52 |
+
model="gpt-4",
|
53 |
+
messages=[{"role": "system", "content": " ".join([str(item) for item in conversation_history])}],
|
54 |
+
temperature=0.3,
|
55 |
+
max_tokens=7500,
|
56 |
+
)
|
57 |
+
|
58 |
+
message = completion.choices[0].message.content
|
59 |
+
print(message)
|
60 |
+
|
61 |
+
initial_prompt()
|
62 |
+
|
63 |
+
def handle_input(input_str : str):
|
64 |
+
global conversation_history
|
65 |
+
|
66 |
+
if len(conversation_history) >=20:
|
67 |
+
conversation_history = conversation_history[:1] + conversation_history[-10:]
|
68 |
+
|
69 |
+
conversation_history.append({"role": "user", "content": f"{input_str}"})
|
70 |
+
|
71 |
+
content = " ".join([str(item) for item in conversation_history])
|
72 |
+
completion = client.chat.completions.create(
|
73 |
+
model="gpt-4",
|
74 |
+
messages=conversation_history,
|
75 |
+
# temperature=temperature,
|
76 |
+
# max_tokens=max_tokens,
|
77 |
+
# presence_penalty=presence_penalty,
|
78 |
+
# frequency_penalty=frequency_penalty,
|
79 |
+
# top_p = top_p_input,
|
80 |
+
#stream = stream_input
|
81 |
+
)
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
message = completion.choices[0].message.content
|
86 |
+
|
87 |
+
conversation_history.append({"role": "assistant", "content": f"{message}"})
|
88 |
+
|
89 |
+
def get_response_again(content):
|
90 |
+
|
91 |
+
while True:
|
92 |
+
completion = client.chat.completion.create(
|
93 |
+
model="gpt-4",
|
94 |
+
messages=[{"role": "assistant", "content": content}],
|
95 |
+
# temperature=temperature,
|
96 |
+
# max_tokens=max_tokens,
|
97 |
+
# presence_penalty=presence_penalty,
|
98 |
+
# frequency_penalty=frequency_penalty,
|
99 |
+
# top_p = top_p_input,
|
100 |
+
#stream = stream_input
|
101 |
+
)
|
102 |
+
|
103 |
+
message = completion.choices[0].message.content
|
104 |
+
|
105 |
+
if "Müşteri" not in message:
|
106 |
+
break
|
107 |
+
|
108 |
+
return message
|
109 |
+
|
110 |
+
# if "Müşteri" in message:
|
111 |
+
# get_response_again(content)
|
112 |
+
|
113 |
+
# conversation_history.append(f"{message}\n")
|
114 |
+
|
115 |
+
return message
|
116 |
+
|
117 |
+
def get_model_reply(query,context=[]):
|
118 |
+
|
119 |
+
context += [query]
|
120 |
+
|
121 |
+
# client.api_key = api_key
|
122 |
+
|
123 |
+
response = handle_input(query)
|
124 |
+
|
125 |
+
|
126 |
+
context += [response]
|
127 |
+
|
128 |
+
responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
|
129 |
+
|
130 |
+
return responses, context
|
131 |
+
|
132 |
+
|
133 |
+
#TODO Thiws feature will be added later
|
134 |
+
def speech_2_text(audio,api_key, context=[]):
|
135 |
+
client.api_key = api_key
|
136 |
+
audio_file= open(audio, "rb")
|
137 |
+
transcript = client.Audio.transcribe("whisper-1", audio_file)
|
138 |
+
prompt = transcript.text
|
139 |
+
context += [prompt]
|
140 |
+
|
141 |
+
completion = client.chat.completion.create(
|
142 |
+
model="gpt-4",
|
143 |
+
messages=[{"role": "user", "content": prompt}],
|
144 |
+
max_tokens=2500,
|
145 |
+
)
|
146 |
+
|
147 |
+
response = completion.choices[0].message.content
|
148 |
+
context += [response]
|
149 |
+
|
150 |
+
responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
|
151 |
+
|
152 |
+
return responses, context
|
153 |
+
|
154 |
+
# def get_audio(url,api_key,context=[]):
|
155 |
+
client.api_key = api_key
|
156 |
+
destination = f"./audio/test"
|
157 |
+
try:
|
158 |
+
ydl_opts = {
|
159 |
+
'format': 'bestaudio/best',
|
160 |
+
'outtmpl':destination,
|
161 |
+
'postprocessors': [{
|
162 |
+
'key': 'FFmpegExtractAudio',
|
163 |
+
'preferredcodec': 'mp3',
|
164 |
+
'preferredquality': '192',
|
165 |
+
}],
|
166 |
+
}
|
167 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
168 |
+
ydl.download([url])
|
169 |
+
|
170 |
+
audio_file= open(f'{destination}.mp3', "rb")
|
171 |
+
transcript = client.Audio.transcribe("whisper-1", audio_file)
|
172 |
+
|
173 |
+
context += [transcript.text]
|
174 |
+
|
175 |
+
responses = [(u,b) for u,b in zip(context[::2], context[1::2])]
|
176 |
+
|
177 |
+
return responses, context
|
178 |
+
|
179 |
+
except Exception as e:
|
180 |
+
print("Connection Error")
|
181 |
+
|
182 |
+
|
183 |
+
with gr.Blocks(css="#chatbot {overflow-y:auto; height:400px;}") as dialog_app:
|
184 |
+
|
185 |
+
with gr.Tab("ChatBot"):
|
186 |
+
with gr.Row():
|
187 |
+
# with gr.Column(scale=1, min_width=600):
|
188 |
+
# api_key = gr.Textbox(label="Your API Key", type="password")
|
189 |
+
# temperature_input = gr.Slider(minimum=0, maximum=1.0, default=0.5, step=0.01, label="Temperature")
|
190 |
+
# max_tokens_input = gr.inputs.Slider(minimum=1, maximum=2048, default=50, step=10, label="Max Tokens")
|
191 |
+
# top_p_input = gr.inputs.Slider(minimum=0.1, maximum=1.0, default=0.5, step=0.01, label="Top P")
|
192 |
+
# presence_penalty_input = gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.0, step=0.1, label="Presence Penalty")
|
193 |
+
# frequency_penalty_input = gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.0, step=0.1, label="Frequency Penalty")
|
194 |
+
#stream_input = gr.inputs.Checkbox(label="Stream")
|
195 |
+
|
196 |
+
with gr.Column(scale=2, min_width=600):
|
197 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
198 |
+
state = gr.State([])
|
199 |
+
|
200 |
+
txt = gr.Textbox(
|
201 |
+
show_label=False,
|
202 |
+
placeholder="Enter text and press enter",
|
203 |
+
container=False
|
204 |
+
)
|
205 |
+
|
206 |
+
# 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])
|
207 |
+
txt.submit(get_model_reply, [txt, state], [chatbot, state])
|
208 |
+
txt.submit(lambda :"", None, txt)
|
209 |
+
|
210 |
+
|
211 |
+
# with gr.Tab("Voice Chat"):
|
212 |
+
# with gr.Row():
|
213 |
+
# with gr.Column(scale=1, min_width=600):
|
214 |
+
# voice_api_key = gr.Textbox(label="Your API Key", type="password")
|
215 |
+
# voice_state = gr.State([])
|
216 |
+
# youtube_url = gr.Textbox(
|
217 |
+
# show_label=False,
|
218 |
+
# type="text",
|
219 |
+
# placeholder="Enter an Youtube URL")
|
220 |
+
|
221 |
+
# mic_audio = gr.Audio(source="microphone", type="filepath")
|
222 |
+
|
223 |
+
# with gr.Row():
|
224 |
+
# with gr.Column(scale=1, min_width=250):
|
225 |
+
# audio_clean_btn = gr.Button(value='Clean')
|
226 |
+
# with gr.Column(scale=2, min_width=250):
|
227 |
+
# audio_sbmt_btn = gr.Button(value='Submit', variant='primary')
|
228 |
+
|
229 |
+
# with gr.Column(scale=2, min_width=600):
|
230 |
+
# voice_bot = gr.Chatbot(elem_id="chatbot")
|
231 |
+
# #txt_box = gr.Textbox(type="text")
|
232 |
+
# voice_state = gr.State([])
|
233 |
+
|
234 |
+
# txt = gr.Textbox(
|
235 |
+
# show_label=False,
|
236 |
+
# placeholder="Enter text and press enter"
|
237 |
+
# ).style(container=False)
|
238 |
+
|
239 |
+
# #youtube_url.submit(get_audio, [youtube_url,voice_api_key, state], [voice_bot, state])
|
240 |
+
# #audio_clean_btn.click(clean_audio, )
|
241 |
+
# audio_sbmt_btn.click(speech_2_text, inputs=[mic_audio,voice_api_key,voice_state], outputs=[voice_bot, voice_state])
|
242 |
+
|
243 |
+
# 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])
|
244 |
+
# txt.submit(lambda :"", None, txt)
|
245 |
+
|
246 |
+
|
247 |
+
dialog_app.launch()
|