Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import json
|
4 |
-
import uuid
|
5 |
import requests
|
6 |
import xml.etree.ElementTree as ET
|
7 |
|
@@ -27,25 +26,7 @@ for item in root.findall('item'):
|
|
27 |
# name: ilk kelime (marka), item_info: (stok durumu, fiyat)
|
28 |
products.append((name, item_info, full_name))
|
29 |
|
30 |
-
# ------------------ Session Yönetimi ------------------ #
|
31 |
-
SESSION_DIR = "chat_sessions"
|
32 |
-
if not os.path.exists(SESSION_DIR):
|
33 |
-
os.makedirs(SESSION_DIR)
|
34 |
|
35 |
-
def get_session_id():
|
36 |
-
return str(uuid.uuid4())
|
37 |
-
|
38 |
-
def load_session(session_id):
|
39 |
-
filename = os.path.join(SESSION_DIR, f"session_{session_id}.json")
|
40 |
-
if os.path.exists(filename):
|
41 |
-
with open(filename, "r", encoding="utf-8") as f:
|
42 |
-
return json.load(f)
|
43 |
-
return []
|
44 |
-
|
45 |
-
def save_session(session_id, history):
|
46 |
-
filename = os.path.join(SESSION_DIR, f"session_{session_id}.json")
|
47 |
-
with open(filename, "w", encoding="utf-8") as f:
|
48 |
-
json.dump(history, f)
|
49 |
|
50 |
# ------------------ Predict Fonksiyonu ------------------ #
|
51 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[], session_id=""):
|
@@ -54,6 +35,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
54 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
55 |
}
|
56 |
print(f"system message is ^^ {system_msg}")
|
|
|
57 |
|
58 |
multi_turn_message = [
|
59 |
{"role": "system", "content": "Sen bir Bıke Finder asistanısın. Görevin insanların doğru bisiklet modeli seçimini sağlamak. Kişilere sorucağın bir takım sorularala, kişileri tanıyıp, onlara stoklarımızda bulunan bisikletlerin model, boylarını tavsiye edeceksin."},
|
@@ -80,9 +62,10 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
80 |
|
81 |
messages.append({"role": "user", "content": inputs})
|
82 |
|
83 |
-
# o3-mini modeli için payload'i güncelledik
|
|
|
84 |
payload = {
|
85 |
-
"model": "
|
86 |
"messages": messages,
|
87 |
"temperature": 0.7,
|
88 |
"top_p": 0.9,
|
@@ -99,34 +82,25 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
99 |
response = requests.post(API_URL, headers=headers, json=payload, stream=payload["stream"])
|
100 |
print(f"Logging : response code - {response}")
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
partial_words += json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
122 |
-
if token_counter == 0:
|
123 |
-
history.append(" " + partial_words)
|
124 |
-
else:
|
125 |
-
history[-1] = partial_words
|
126 |
-
chat = [(history[i], history[i+1]) for i in range(0, len(history)-1, 2)]
|
127 |
-
token_counter += 1
|
128 |
-
save_session(session_id, history)
|
129 |
-
yield chat, history, chat_counter, response
|
130 |
|
131 |
def reset_textbox():
|
132 |
return gr.update(value='')
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import json
|
|
|
4 |
import requests
|
5 |
import xml.etree.ElementTree as ET
|
6 |
|
|
|
26 |
# name: ilk kelime (marka), item_info: (stok durumu, fiyat)
|
27 |
products.append((name, item_info, full_name))
|
28 |
|
|
|
|
|
|
|
|
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# ------------------ Predict Fonksiyonu ------------------ #
|
32 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[], session_id=""):
|
|
|
35 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
36 |
}
|
37 |
print(f"system message is ^^ {system_msg}")
|
38 |
+
initial_message = [{"role": "user", "content": f"{inputs}"}]
|
39 |
|
40 |
multi_turn_message = [
|
41 |
{"role": "system", "content": "Sen bir Bıke Finder asistanısın. Görevin insanların doğru bisiklet modeli seçimini sağlamak. Kişilere sorucağın bir takım sorularala, kişileri tanıyıp, onlara stoklarımızda bulunan bisikletlerin model, boylarını tavsiye edeceksin."},
|
|
|
62 |
|
63 |
messages.append({"role": "user", "content": inputs})
|
64 |
|
65 |
+
# o3-mini modeli için payload'i güncelledik.
|
66 |
+
# Eğer o3-mini modeli streaming desteklemiyorsa "stream": False yapıyoruz.
|
67 |
payload = {
|
68 |
+
"model": "gpt-4o",
|
69 |
"messages": messages,
|
70 |
"temperature": 0.7,
|
71 |
"top_p": 0.9,
|
|
|
82 |
response = requests.post(API_URL, headers=headers, json=payload, stream=payload["stream"])
|
83 |
print(f"Logging : response code - {response}")
|
84 |
|
85 |
+
token_counter = 0
|
86 |
+
partial_words = ""
|
87 |
+
counter = 0
|
88 |
+
for chunk in response.iter_lines():
|
89 |
+
if counter == 0:
|
90 |
+
counter += 1
|
91 |
+
continue
|
92 |
+
if chunk.decode():
|
93 |
+
chunk = chunk.decode()
|
94 |
+
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
95 |
+
partial_words += json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
96 |
+
if token_counter == 0:
|
97 |
+
history.append(" " + partial_words)
|
98 |
+
else:
|
99 |
+
history[-1] = partial_words
|
100 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
101 |
+
token_counter += 1
|
102 |
+
save_session(session_id, history)
|
103 |
+
yield chat, history, chat_counter, response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
def reset_textbox():
|
106 |
return gr.update(value='')
|