Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,11 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
5 |
-
|
6 |
|
7 |
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
8 |
-
|
9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
10 |
|
11 |
-
|
12 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
13 |
-
|
14 |
headers = {
|
15 |
"Content-Type": "application/json",
|
16 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
@@ -46,15 +42,7 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
46 |
temp["content"] = inputs
|
47 |
messages.append(temp)
|
48 |
|
49 |
-
payload = {
|
50 |
-
"model": "gpt-3.5-turbo",
|
51 |
-
"messages": messages,
|
52 |
-
"temperature" : 0.2,
|
53 |
-
"top_p": 1.0,
|
54 |
-
"n" : 1,
|
55 |
-
"stream": True,
|
56 |
-
"presence_penalty":0,
|
57 |
-
"frequency_penalty":0,}
|
58 |
|
59 |
chat_counter+=1
|
60 |
|
@@ -84,50 +72,32 @@ def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], hi
|
|
84 |
history[-1] = partial_words
|
85 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
86 |
token_counter+=1
|
87 |
-
yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
|
88 |
-
|
89 |
-
|
90 |
def reset_textbox():
|
91 |
return gr.update(value='')
|
92 |
-
|
93 |
-
|
94 |
def set_visible_false():
|
95 |
return gr.update(visible=False)
|
96 |
-
|
97 |
-
|
98 |
def set_visible_true():
|
99 |
return gr.update(visible=False)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
theme_addon_msg = ""
|
105 |
-
|
106 |
system_msg_info = ""
|
107 |
-
|
108 |
-
|
109 |
theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue",
|
110 |
text_size=gr.themes.sizes.text_md)
|
111 |
|
112 |
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 360px; overflow: auto;}""",
|
113 |
-
theme=theme) as demo:
|
114 |
-
|
115 |
-
with gr.Column(elem_id = "col_container"):
|
116 |
-
|
117 |
with gr.Accordion("", open=False, visible=False):
|
118 |
system_msg = gr.Textbox(value="")
|
119 |
accordion_msg = gr.HTML(value="", visible=False)
|
120 |
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
121 |
inputs = gr.Textbox(placeholder= "Buraya yazın, yanıtlayalım.", show_label= False)
|
122 |
-
state = gr.State([])
|
123 |
-
|
124 |
-
|
125 |
with gr.Accordion("", open=False, visible=False):
|
126 |
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=False, visible=False)
|
127 |
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False)
|
128 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
129 |
|
130 |
-
|
131 |
inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],) #openai_api_key
|
132 |
inputs.submit(reset_textbox, [], [inputs])
|
133 |
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import requests
|
|
|
5 |
|
6 |
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
|
|
7 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
8 |
|
|
|
9 |
def predict(system_msg, inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
|
|
10 |
headers = {
|
11 |
"Content-Type": "application/json",
|
12 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
|
|
42 |
temp["content"] = inputs
|
43 |
messages.append(temp)
|
44 |
|
45 |
+
payload = {"model": "gpt-3.5-turbo", "messages": messages, "temperature" : 0.2, "top_p": 1.0, "n" : 1, "stream": True, "presence_penalty":0, "frequency_penalty":0,}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
chat_counter+=1
|
48 |
|
|
|
72 |
history[-1] = partial_words
|
73 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
74 |
token_counter+=1
|
75 |
+
yield chat, history, chat_counter, response # resembles {chatbot: chat, state: history}
|
|
|
|
|
76 |
def reset_textbox():
|
77 |
return gr.update(value='')
|
|
|
|
|
78 |
def set_visible_false():
|
79 |
return gr.update(visible=False)
|
|
|
|
|
80 |
def set_visible_true():
|
81 |
return gr.update(visible=False)
|
|
|
|
|
|
|
|
|
82 |
theme_addon_msg = ""
|
|
|
83 |
system_msg_info = ""
|
|
|
|
|
84 |
theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="green", neutral_hue="blue",
|
85 |
text_size=gr.themes.sizes.text_md)
|
86 |
|
87 |
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 360px; overflow: auto;}""",
|
88 |
+
theme=theme) as demo:
|
89 |
+
with gr.Column(elem_id = "col_container"):
|
|
|
|
|
90 |
with gr.Accordion("", open=False, visible=False):
|
91 |
system_msg = gr.Textbox(value="")
|
92 |
accordion_msg = gr.HTML(value="", visible=False)
|
93 |
chatbot = gr.Chatbot(label='Trek Asistanı', elem_id="chatbot")
|
94 |
inputs = gr.Textbox(placeholder= "Buraya yazın, yanıtlayalım.", show_label= False)
|
95 |
+
state = gr.State([])
|
|
|
|
|
96 |
with gr.Accordion("", open=False, visible=False):
|
97 |
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=False, visible=False)
|
98 |
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=False, visible=False)
|
99 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
100 |
|
|
|
101 |
inputs.submit( predict, [system_msg, inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],) #openai_api_key
|
102 |
inputs.submit(reset_textbox, [], [inputs])
|
103 |
|