Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, jsonify, render_template, request, send_file
|
2 |
+
from slack_sdk import WebClient
|
3 |
+
from slack_sdk.errors import SlackApiError
|
4 |
+
import os
|
5 |
+
from slack_bolt import App
|
6 |
+
import flask
|
7 |
+
import requests
|
8 |
+
import json
|
9 |
+
app = flask.Flask(__name__, template_folder="static")
|
10 |
+
def text_generate(prompt):
|
11 |
+
url = "http://34.122.217.42/complete_batch"
|
12 |
+
data = {
|
13 |
+
"debug_ext_path": "foo",
|
14 |
+
"context":prompt,
|
15 |
+
"top_p": 0.9,
|
16 |
+
"temp": 0.75}
|
17 |
+
try:
|
18 |
+
response = requests.post(url, json=data)
|
19 |
+
if response.status_code == 200:
|
20 |
+
data_str=response.json()
|
21 |
+
slack_msg="Supplied Prompt was :"+data["context"]+"\n\n"
|
22 |
+
slack_msg = slack_msg+"Response receievd was : \n\n"
|
23 |
+
for i in range(4):
|
24 |
+
slack_msg = slack_msg+data_str["completions"][i]["completion"]+"\n\n"
|
25 |
+
slack_msg = slack_msg+data_str["completions"][i]["log_prob"]+"\n\n"
|
26 |
+
else:
|
27 |
+
slack_msg="Failed to send JSON message to URL"
|
28 |
+
except requests.exceptions.Timeout:
|
29 |
+
slack_msg="Failed to send JSON message to URL due to timeout error"
|
30 |
+
pass
|
31 |
+
return slack_msg
|
32 |
+
@app.route("/")
|
33 |
+
def index():
|
34 |
+
return 'Hello'
|
35 |
+
|
36 |
+
@app.route("/events", methods=["POST"])
|
37 |
+
def handle_event():
|
38 |
+
# Handle the event here
|
39 |
+
event=request.json['type']
|
40 |
+
|
41 |
+
if event=='url_verification':
|
42 |
+
# Respond to the challenge request with a 200 OK HTTP status code
|
43 |
+
# and the value of the challenge parameter in the response body
|
44 |
+
challenge = request.json['challenge']
|
45 |
+
return (
|
46 |
+
challenge
|
47 |
+
)
|
48 |
+
if event=='app_mention':
|
49 |
+
API_KEY=os.environ.get('SLACK_APP_TOKEN')
|
50 |
+
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL')
|
51 |
+
# Set up the Slack client
|
52 |
+
YOUR_BOT_TOKEN=API_KEY
|
53 |
+
client = WebClient(token=YOUR_BOT_TOKEN)
|
54 |
+
channel= SLACK_CHANNEL
|
55 |
+
conversation = request.json['text']
|
56 |
+
print(conversation)
|
57 |
+
response=text_generate(conversation)
|
58 |
+
# Post the response back to the Slack channel
|
59 |
+
try:
|
60 |
+
client.chat_postMessage(
|
61 |
+
channel='#chat-gpt-bot',
|
62 |
+
text=response
|
63 |
+
)
|
64 |
+
except SlackApiError as e:
|
65 |
+
response="Error"
|
66 |
+
return response
|
67 |
+
if event=='event_callback':
|
68 |
+
API_KEY=os.environ.get('SLACK_APP_TOKEN')
|
69 |
+
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL')
|
70 |
+
# Set up the Slack client
|
71 |
+
YOUR_BOT_TOKEN=API_KEY
|
72 |
+
client = WebClient(token=YOUR_BOT_TOKEN)
|
73 |
+
channel= SLACK_CHANNEL
|
74 |
+
conversation = request.json['event'] ['text']
|
75 |
+
print(conversation)
|
76 |
+
response=text_generate(conversation)
|
77 |
+
# Post the response back to the Slack channel
|
78 |
+
try:
|
79 |
+
client.chat_postMessage(
|
80 |
+
channel=channel,
|
81 |
+
text=response
|
82 |
+
)
|
83 |
+
except SlackApiError as e:
|
84 |
+
response="Error"
|
85 |
+
return response
|
86 |
+
if __name__ == "__main__":
|
87 |
+
app.run(host="0.0.0.0", port=7860)
|