SamiKoen commited on
Commit
04b6f7b
·
verified ·
1 Parent(s): c051efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -9
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import os
3
  import json
 
4
  import requests
5
  import xml.etree.ElementTree as ET
6
 
@@ -26,7 +27,28 @@ for item in root.findall('item'):
26
  # name: ilk kelime (marka), item_info: (stok durumu, fiyat)
27
  products.append((name, item_info, full_name))
28
 
29
- def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  headers = {
31
  "Content-Type": "application/json",
32
  "Authorization": f"Bearer {OPENAI_API_KEY}"
@@ -59,13 +81,15 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
59
 
60
  messages.append({"role": "user", "content": inputs})
61
 
 
 
62
  payload = {
63
- "model": "gpt-4o",
64
  "messages": messages,
65
  "temperature": 0.7,
66
  "top_p": 0.9,
67
  "n": 1,
68
- "stream": True,
69
  "presence_penalty": 0,
70
  "frequency_penalty": 0,
71
  }
@@ -74,7 +98,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
74
  history.append(inputs)
75
  print(f"Logging : payload is - {payload}")
76
 
77
- response = requests.post(API_URL, headers=headers, json=payload, stream=True)
78
  print(f"Logging : response code - {response}")
79
 
80
  token_counter = 0
@@ -94,6 +118,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
94
  history[-1] = partial_words
95
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
96
  token_counter += 1
 
97
  yield chat, history, chat_counter, response
98
 
99
  def reset_textbox():
@@ -190,8 +215,10 @@ theme = gr.themes.Base(
190
  spacing_size="sm",
191
  )
192
 
 
 
 
193
  with gr.Blocks(theme=theme, css=css) as demo:
194
- # Modern chat başlığı
195
  gr.Markdown("<div class='chat-header'>Trek Bike Finder Chatbot</div>")
196
 
197
  with gr.Column(elem_id="col_container"):
@@ -199,11 +226,13 @@ with gr.Blocks(theme=theme, css=css) as demo:
199
  system_msg = gr.Textbox(value="")
200
  new_msg = gr.Textbox(value="")
201
  accordion_msg = gr.HTML(value="", visible=False)
202
- # Gradio Chatbot bileşeni; CSS ile stil veriliyor.
203
  chatbot = gr.Chatbot(label='Trek Bike Finder', elem_id="chatbot")
204
  inputs = gr.Textbox(
205
  placeholder="Buraya yazın, istediğiniz modeli beraber bulalım.", show_label=False)
206
- state = gr.State([])
 
 
 
207
  with gr.Accordion("", open=False, visible=False):
208
  top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
209
  temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
@@ -211,9 +240,9 @@ with gr.Blocks(theme=theme, css=css) as demo:
211
 
212
  inputs.submit(
213
  predict,
214
- [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state],
215
  [chatbot, state, chat_counter]
216
  )
217
  inputs.submit(reset_textbox, [], [inputs])
218
 
219
- demo.queue(max_size=10).launch(debug=True)
 
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
  # 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=""):
52
  headers = {
53
  "Content-Type": "application/json",
54
  "Authorization": f"Bearer {OPENAI_API_KEY}"
 
81
 
82
  messages.append({"role": "user", "content": inputs})
83
 
84
+ # o3-mini modeli için payload'i güncelledik.
85
+ # Eğer o3-mini modeli streaming desteklemiyorsa "stream": False yapıyoruz.
86
  payload = {
87
+ "model": "o3-mini",
88
  "messages": messages,
89
  "temperature": 0.7,
90
  "top_p": 0.9,
91
  "n": 1,
92
+ "stream": False,
93
  "presence_penalty": 0,
94
  "frequency_penalty": 0,
95
  }
 
98
  history.append(inputs)
99
  print(f"Logging : payload is - {payload}")
100
 
101
+ response = requests.post(API_URL, headers=headers, json=payload, stream=payload["stream"])
102
  print(f"Logging : response code - {response}")
103
 
104
  token_counter = 0
 
118
  history[-1] = partial_words
119
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
120
  token_counter += 1
121
+ save_session(session_id, history)
122
  yield chat, history, chat_counter, response
123
 
124
  def reset_textbox():
 
215
  spacing_size="sm",
216
  )
217
 
218
+ # Yeni oturum için benzersiz session ID oluşturuyoruz
219
+ session_id_value = get_session_id()
220
+
221
  with gr.Blocks(theme=theme, css=css) as demo:
 
222
  gr.Markdown("<div class='chat-header'>Trek Bike Finder Chatbot</div>")
223
 
224
  with gr.Column(elem_id="col_container"):
 
226
  system_msg = gr.Textbox(value="")
227
  new_msg = gr.Textbox(value="")
228
  accordion_msg = gr.HTML(value="", visible=False)
 
229
  chatbot = gr.Chatbot(label='Trek Bike Finder', elem_id="chatbot")
230
  inputs = gr.Textbox(
231
  placeholder="Buraya yazın, istediğiniz modeli beraber bulalım.", show_label=False)
232
+ # Oturum geçmişini session_id üzerinden yüklüyoruz.
233
+ state = gr.State(load_session(session_id_value))
234
+ # Gizli session ID
235
+ session_id = gr.Textbox(value=session_id_value, visible=False, label="Session ID")
236
  with gr.Accordion("", open=False, visible=False):
237
  top_p = gr.Slider(minimum=0, maximum=1.0, value=0.5, step=0.05, interactive=False, visible=False)
238
  temperature = gr.Slider(minimum=0, maximum=5.0, value=0.1, step=0.1, interactive=False, visible=False)
 
240
 
241
  inputs.submit(
242
  predict,
243
+ [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state, session_id],
244
  [chatbot, state, chat_counter]
245
  )
246
  inputs.submit(reset_textbox, [], [inputs])
247
 
248
+ demo.queue(max_size=10).launch(debug=True)