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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -54,7 +54,6 @@ 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
- initial_message = [{"role": "user", "content": f"{inputs}"}]
58
 
59
  multi_turn_message = [
60
  {"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."},
@@ -81,8 +80,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
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,
@@ -101,25 +99,34 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
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
105
- partial_words = ""
106
- counter = 0
107
- for chunk in response.iter_lines():
108
- if counter == 0:
109
- counter += 1
110
- continue
111
- if chunk.decode():
112
- chunk = chunk.decode()
113
- if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
114
- partial_words += json.loads(chunk[6:])['choices'][0]["delta"]["content"]
115
- if token_counter == 0:
116
- history.append(" " + partial_words)
117
- else:
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():
125
  return gr.update(value='')
 
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
 
81
  messages.append({"role": "user", "content": inputs})
82
 
83
+ # o3-mini modeli için payload'i güncelledik; stream False
 
84
  payload = {
85
  "model": "o3-mini",
86
  "messages": messages,
 
99
  response = requests.post(API_URL, headers=headers, json=payload, stream=payload["stream"])
100
  print(f"Logging : response code - {response}")
101
 
102
+ if not payload["stream"]:
103
+ # Normal JSON yanıtı işleme (o3-mini için)
104
+ result = response.json()
105
+ full_response = result["choices"][0]["message"]["content"]
106
+ history.append(full_response)
107
+ chat = [(history[i], history[i+1]) for i in range(0, len(history)-1, 2)]
108
+ save_session(session_id, history)
109
+ return chat, history, chat_counter
110
+ else:
111
+ token_counter = 0
112
+ partial_words = ""
113
+ counter = 0
114
+ for chunk in response.iter_lines():
115
+ if counter == 0:
116
+ counter += 1
117
+ continue
118
+ if chunk.decode():
119
+ chunk = chunk.decode()
120
+ if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
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='')