SamiKoen commited on
Commit
3c640a3
·
1 Parent(s): b843cfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -4
app.py CHANGED
@@ -3,7 +3,6 @@ import os
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
6
- from collections import defaultdict
7
  from io import StringIO
8
 
9
  # API ayarları
@@ -13,8 +12,6 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
  # XML verisinin URL'si
14
  xml_url = "http://www.alatin.com.tr/index.php?do=catalog/output&pCode=4249932787" # Gerçek URL'nizi buraya yazın
15
 
16
-
17
-
18
  # XML verisini URL'den çek
19
  response = requests.get(xml_url)
20
  response.raise_for_status()
@@ -41,6 +38,7 @@ for item in root.findall('item'):
41
 
42
  # Tüm 'item' metinlerini birleştir
43
  full_text = '\n\n'.join(items_text)
 
44
  def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
45
  headers = {
46
  "Content-Type": "application/json",
@@ -87,4 +85,57 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
87
  history.append(inputs)
88
  print(f"Logging : payload is - {payload}")
89
 
90
- response = requests.post(API_URL, headers=headers, json=payload, stream=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import json
4
  import requests
5
  import xml.etree.ElementTree as ET
 
6
  from io import StringIO
7
 
8
  # API ayarları
 
12
  # XML verisinin URL'si
13
  xml_url = "http://www.alatin.com.tr/index.php?do=catalog/output&pCode=4249932787" # Gerçek URL'nizi buraya yazın
14
 
 
 
15
  # XML verisini URL'den çek
16
  response = requests.get(xml_url)
17
  response.raise_for_status()
 
38
 
39
  # Tüm 'item' metinlerini birleştir
40
  full_text = '\n\n'.join(items_text)
41
+
42
  def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
43
  headers = {
44
  "Content-Type": "application/json",
 
85
  history.append(inputs)
86
  print(f"Logging : payload is - {payload}")
87
 
88
+ response = requests.post(API_URL, headers=headers, json=payload, stream=True)
89
+
90
+ print(f"Logging : response code - {response}")
91
+ token_counter = 0
92
+ partial_words = ""
93
+
94
+ counter=0
95
+ for chunk in response.iter_lines():
96
+
97
+ if counter == 0:
98
+ counter+=1
99
+ continue
100
+
101
+ if chunk.decode() :
102
+ chunk = chunk.decode()
103
+
104
+ if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
105
+ partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
106
+ if token_counter == 0:
107
+ history.append(" " + partial_words)
108
+ else:
109
+ history[-1] = partial_words
110
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
111
+ token_counter+=1
112
+ yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
113
+ def reset_textbox():
114
+ return gr.update(value='')
115
+ def set_visible_false():
116
+ return gr.update(visible=False)
117
+ def set_visible_true():
118
+ return gr.update(visible=False)
119
+ theme_addon_msg = ""
120
+ system_msg_info = ""
121
+ theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue",
122
+ text_size=gr.themes.sizes.text_md)
123
+
124
+ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 450px; overflow: auto;}""",
125
+ theme=theme) as demo:
126
+ with gr.Column(elem_id = "col_container"):
127
+ with gr.Accordion("", open=False, visible=False):
128
+ system_msg = gr.Textbox(value="")
129
+ accordion_msg = gr.HTML(value="", visible=False)
130
+ chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
131
+ inputs = gr.Textbox(placeholder= "Buraya yazın, yanıtlayalım.", show_label= False)
132
+ state = gr.State([])
133
+ with gr.Accordion("", open=False, visible=False):
134
+ top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=False, visible=False)
135
+ temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False)
136
+ chat_counter = gr.Number(value=0, visible=False, precision=0)
137
+
138
+ inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],) #openai_api_key
139
+ inputs.submit(reset_textbox, [], [inputs])
140
+
141
+ demo.queue(max_size=20, concurrency_count=20).launch(debug=True)