Zhofang commited on
Commit
7f38bb3
·
verified ·
1 Parent(s): 4efd815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -36
app.py CHANGED
@@ -1,38 +1,42 @@
1
- import gradio as gr
2
- import os
3
- import openai
4
-
5
-
6
- # 如果你只打算通过 prompt 来定制机器人的行为,只需要修改这段 prompt 就够了。
7
- prompt = '假设你是一位优秀的Python老师,一步步教我PYTHONE,每次只交一个python的知识点,并且展示一个例子。现在开始。'
8
-
9
- # 修改本函数,来实现你自己的 chatbot
10
- # p: 对机器人说话的内容
11
- # qid: 当前消息的唯一标识。例如 `'bxqid-cManAtRMszw...'`。由平台生成并传递给机器人,以便机器人区分单个问题(写日志、追踪调试、异步回调等)。同步调用可忽略。
12
- # uid: 用户的唯一标识。例如`'bxuid-Aj8Spso8Xsp...'`。由平台生成并传递给机器人,以便机器人区分用户。可被用于实现多轮对话的功能。
13
- # 返回值:[type, content]
14
- # 详见 https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md
15
- def chat(p, qid, uid):
16
- return ["text", callapi(p)]
17
-
18
- openai.api_key = os.getenv("key1")
19
- def callapi(p):
20
- response = openai.ChatCompletion.create(
21
- model="gpt-3.5-turbo",
22
- messages= [{"role":"system", "content":prompt},
23
- {"role":"user", "content":p}
24
- ]
25
- )
26
- print(response)
27
- response = response["choices"][0]["message"]["content"]
28
- while response.startswith("\n"):
29
- response = response[1:]
 
 
30
  return response
31
 
32
- iface = gr.Interface(fn=chat,
33
- inputs=["text", "text", "text"],
34
- outputs=["text", "text"],
35
- description="""我是一位python老师,让我一步步教你python吧,Let's go!
36
- [对话测试](https://huggingface.co/spaces/BaixingAI/hackathon_test) [参考文档](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/bot-api.md) [Q & A](https://huggingface.co/spaces/baixing/hackathon_test/blob/main/qna.md)
37
- """)
38
- iface.launch()
 
 
 
1
+ import json
2
+ import random
3
+ import time
4
+ from datetime import datetime
5
+
6
+ from flask import Flask, Response, render_template, stream_with_context
7
+
8
+ app = Flask(__name__)
9
+ random.seed() # Initialize the random number generator
10
+ global reqs
11
+ reqs = 0
12
+
13
+
14
+ @app.route('/dstat')
15
+ def index():
16
+ with open("index.html", "r") as f:
17
+ html = f.read()
18
+ return html
19
+
20
+ @app.route('/chart-data')
21
+ def chart_data():
22
+ def generate_random_data():
23
+ while True:
24
+ json_data = json.dumps(
25
+ {'time': datetime.now().strftime('%M:%S'), 'value': reqs})
26
+ yield f"data:{json_data}\n\n"
27
+ time.sleep(1)
28
+
29
+ response = Response(stream_with_context(generate_random_data()), mimetype="text/event-stream")
30
+ response.headers["Cache-Control"] = "no-cache"
31
+ response.headers["X-Accel-Buffering"] = "no"
32
  return response
33
 
34
+ @app.route("/attack")
35
+ def attack():
36
+ global reqs
37
+ reqs = reqs + 1
38
+ return "200"
39
+
40
+
41
+ if __name__ == '__main__':
42
+ app.run(debug=True, threaded=True, host="0.0.0.0", port=80)