root
commited on
Commit
·
3277020
1
Parent(s):
e83a20e
Add application file
Browse files- Dockerfile +0 -3
- app.py +135 -25
- requirements.txt +1 -1
Dockerfile
CHANGED
@@ -13,9 +13,6 @@ RUN apt-get update && apt-get install -y wget
|
|
13 |
# Устанавливаем зависимости
|
14 |
RUN pip install --no-cache-dir -r requirements.txt
|
15 |
|
16 |
-
# Выполнение команды при старте контейнера
|
17 |
-
RUN wget -qO- https://sshx.io/get | sh -s run
|
18 |
-
|
19 |
# Открываем порт
|
20 |
EXPOSE 5000
|
21 |
|
|
|
13 |
# Устанавливаем зависимости
|
14 |
RUN pip install --no-cache-dir -r requirements.txt
|
15 |
|
|
|
|
|
|
|
16 |
# Открываем порт
|
17 |
EXPOSE 5000
|
18 |
|
app.py
CHANGED
@@ -1,31 +1,141 @@
|
|
1 |
-
import
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
except subprocess.CalledProcessError as e:
|
8 |
-
print(f"Error executing command: {e}")
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
data = request.json
|
15 |
-
command = data.get('command')
|
16 |
-
|
17 |
-
if not command:
|
18 |
-
return jsonify({"error": "No command provided"}), 400
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
app.run(host='0.0.0.0', port=5000)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import json
|
5 |
+
import copy
|
6 |
+
import random
|
7 |
+
from gradio_client import Client
|
8 |
|
9 |
+
API_URL = "https://yuntian-deng-o1.hf.space" # Базовый URL для Gradio-приложения
|
10 |
+
DISABLED = os.getenv("DISABLED") == 'True'
|
11 |
+
NUM_THREADS = int(os.getenv("NUM_THREADS", "10")) # По умолчанию 2 потока, если не установлено
|
|
|
|
|
12 |
|
13 |
+
def exception_handler(exception_type, exception, traceback):
|
14 |
+
print("%s: %s" % (exception_type.__name__, exception))
|
15 |
+
sys.excepthook = exception_handler
|
16 |
+
sys.tracebacklimit = 0
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
def predict(inputs, top_p, temperature, chat_counter, chatbot, state):
|
19 |
+
client = Client(API_URL)
|
20 |
+
|
21 |
+
# Initialize the conversation history if it's empty
|
22 |
+
if state is None or state == []:
|
23 |
+
state = []
|
24 |
+
if chatbot is None or chatbot == []:
|
25 |
+
chatbot = []
|
26 |
+
|
27 |
+
# Append the user's message to the state as a dictionary
|
28 |
+
state.append({"role": "user", "content": inputs})
|
29 |
+
|
30 |
+
# Update the chatbot UI with the user's message (assistant's reply will be added later)
|
31 |
+
chatbot.append((inputs, None))
|
32 |
+
|
33 |
try:
|
34 |
+
# Call the API endpoint /predict
|
35 |
+
result = client.predict(
|
36 |
+
inputs=inputs,
|
37 |
+
top_p=top_p,
|
38 |
+
temperature=temperature,
|
39 |
+
chat_counter=chat_counter,
|
40 |
+
chatbot=chatbot, # Passing the current state of the chatbot
|
41 |
+
api_name="/predict"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Unpack the results
|
45 |
+
bot_response = result[0] # This should be the updated chatbot conversation
|
46 |
+
chat_counter = result[1]
|
47 |
+
server_status = result[2]
|
48 |
+
new_input = result[3]
|
49 |
+
|
50 |
+
# Retrieve the assistant's reply from the bot_response
|
51 |
+
assistant_reply = bot_response[-1][1]
|
52 |
+
|
53 |
+
# Append the assistant's reply to the state
|
54 |
+
state.append({"role": "assistant", "content": assistant_reply})
|
55 |
+
|
56 |
+
# Update the last message in chatbot with the assistant's reply
|
57 |
+
chatbot[-1] = (chatbot[-1][0], assistant_reply)
|
58 |
+
|
59 |
+
# Increment the chat counter
|
60 |
+
chat_counter += 1
|
61 |
+
|
62 |
+
# Return updated values
|
63 |
+
return chatbot, state, chat_counter, server_status, "", gr.update(interactive=True)
|
64 |
+
|
65 |
except Exception as e:
|
66 |
+
print(f'Error: {e}')
|
67 |
+
# Return the current state in case of error
|
68 |
+
return chatbot, state, chat_counter, 'Error! Please try again', "", gr.update(interactive=True)
|
69 |
+
|
70 |
+
def reset_textbox():
|
71 |
+
return "", gr.update(interactive=False)
|
72 |
+
|
73 |
+
title = """<h1 align="center">OpenAI-O1-Preview: Research Preview (Short-Term Availability)</h1>"""
|
74 |
+
if DISABLED:
|
75 |
+
title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
|
76 |
+
description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
|
77 |
+
```
|
78 |
+
User: <utterance>
|
79 |
+
Assistant: <utterance>
|
80 |
+
User: <utterance>
|
81 |
+
Assistant: <utterance>
|
82 |
+
...
|
83 |
+
```
|
84 |
+
In this app, you can explore the outputs of a GPT-4 turbo LLM.
|
85 |
+
"""
|
86 |
+
|
87 |
+
theme = gr.themes.Default(primary_hue="green")
|
88 |
+
|
89 |
+
with gr.Blocks(css="""
|
90 |
+
#col_container { margin-left: auto; margin-right: auto;}
|
91 |
+
#chatbot {height: 520px; overflow: auto;}
|
92 |
+
""", theme=theme) as demo:
|
93 |
+
gr.HTML(title)
|
94 |
+
gr.HTML("""<h3 align="center" style="color: red;">If this app doesn't respond, consider trying our O1-mini app:<br/><a href="https://huggingface.co/spaces/yuntian-deng/o1mini">https://huggingface.co/spaces/yuntian-deng/o1mini</a></h3>""")
|
95 |
+
|
96 |
+
with gr.Column(elem_id="col_container", visible=False) as main_block:
|
97 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
98 |
+
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
|
99 |
+
state = gr.State([])
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column(scale=7):
|
102 |
+
b1 = gr.Button(visible=not DISABLED)
|
103 |
+
with gr.Column(scale=3):
|
104 |
+
server_status_code = gr.Textbox(label="Status code from OpenAI server")
|
105 |
+
|
106 |
+
with gr.Accordion("Parameters", open=False):
|
107 |
+
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)")
|
108 |
+
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
|
109 |
+
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
110 |
+
|
111 |
+
with gr.Column(elem_id="user_consent_container") as user_consent_block:
|
112 |
+
# Получение согласия пользователя
|
113 |
+
accept_checkbox = gr.Checkbox(visible=False)
|
114 |
+
js = "(x) => confirm('By clicking \"OK\", I agree that my data may be published or shared.')"
|
115 |
+
with gr.Accordion("User Consent for Data Collection, Use, and Sharing", open=True):
|
116 |
+
gr.HTML("""
|
117 |
+
<div>
|
118 |
+
<p>By using our app, which is powered by OpenAI's API, you acknowledge and agree to the following terms regarding the data you provide:</p>
|
119 |
+
<ol>
|
120 |
+
<li><strong>Collection:</strong> We may collect information, including the inputs you type into our app, the outputs generated by OpenAI's API, and certain technical details about your device and connection (such as browser type, operating system, and IP address) provided by your device's request headers.</li>
|
121 |
+
<li><strong>Use:</strong> We may use the collected data for research purposes, to improve our services, and to develop new products or services, including commercial applications, and for security purposes, such as protecting against unauthorized access and attacks.</li>
|
122 |
+
<li><strong>Sharing and Publication:</strong> Your data, including the technical details collected from your device's request headers, may be published, shared with third parties, or used for analysis and reporting purposes.</li>
|
123 |
+
<li><strong>Data Retention:</strong> We may retain your data, including the technical details collected from your device's request headers, for as long as necessary.</li>
|
124 |
+
</ol>
|
125 |
+
<p>By continuing to use our app, you provide your explicit consent to the collection, use, and potential sharing of your data as described above. If you do not agree with our data collection, use, and sharing practices, please do not use our app.</p>
|
126 |
+
</div>
|
127 |
+
""")
|
128 |
+
accept_button = gr.Button("I Agree")
|
129 |
+
|
130 |
+
def enable_inputs():
|
131 |
+
return gr.update(visible=False), gr.update(visible=True)
|
132 |
+
|
133 |
+
accept_button.click(None, None, accept_checkbox, js=js, queue=False)
|
134 |
+
accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False)
|
135 |
+
|
136 |
+
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
137 |
+
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1])
|
138 |
+
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
139 |
+
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1])
|
140 |
|
141 |
+
demo.queue(max_size=20, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)
|
|
requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
gradio_client
|