Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 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"]
|
| 25 |
+
slack_msg = slack_msg+data_str["completions"][i]["log_prob"])
|
| 26 |
+
else:
|
| 27 |
+
slack_msg="Failed to send JSON message to URL"+response.status_code
|
| 28 |
+
return slack_msg
|
| 29 |
+
|
| 30 |
+
@app.route("/")
|
| 31 |
+
def index():
|
| 32 |
+
return 'Hello'
|
| 33 |
+
|
| 34 |
+
@app.route("/events", methods=["POST"])
|
| 35 |
+
def handle_event():
|
| 36 |
+
# Handle the event here
|
| 37 |
+
event=request.json['type']
|
| 38 |
+
|
| 39 |
+
if event=='url_verification':
|
| 40 |
+
# Respond to the challenge request with a 200 OK HTTP status code
|
| 41 |
+
# and the value of the challenge parameter in the response body
|
| 42 |
+
challenge = request.json['challenge']
|
| 43 |
+
return (
|
| 44 |
+
challenge
|
| 45 |
+
)
|
| 46 |
+
if event=='app_mention':
|
| 47 |
+
API_KEY=os.environ.get('SLACK_APP_TOKEN')
|
| 48 |
+
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL')
|
| 49 |
+
# Set up the Slack client
|
| 50 |
+
YOUR_BOT_TOKEN=API_KEY
|
| 51 |
+
client = WebClient(token=YOUR_BOT_TOKEN)
|
| 52 |
+
channel= SLACK_CHANNEL
|
| 53 |
+
conversation = request.json['text']
|
| 54 |
+
print(conversation)
|
| 55 |
+
response=text_generate(conversation)
|
| 56 |
+
# Post the response back to the Slack channel
|
| 57 |
+
try:
|
| 58 |
+
client.chat_postMessage(
|
| 59 |
+
channel='#chat-gpt-bot',
|
| 60 |
+
text=response
|
| 61 |
+
)
|
| 62 |
+
except SlackApiError as e:
|
| 63 |
+
response="Error"
|
| 64 |
+
return response
|
| 65 |
+
if event=='event_callback':
|
| 66 |
+
API_KEY=os.environ.get('SLACK_APP_TOKEN')
|
| 67 |
+
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL')
|
| 68 |
+
# Set up the Slack client
|
| 69 |
+
YOUR_BOT_TOKEN=API_KEY
|
| 70 |
+
client = WebClient(token=YOUR_BOT_TOKEN)
|
| 71 |
+
channel= SLACK_CHANNEL
|
| 72 |
+
conversation = request.json['event'] ['text']
|
| 73 |
+
print(conversation)
|
| 74 |
+
response=text_generate(conversation)
|
| 75 |
+
# Post the response back to the Slack channel
|
| 76 |
+
try:
|
| 77 |
+
client.chat_postMessage(
|
| 78 |
+
channel=channel,
|
| 79 |
+
text=response
|
| 80 |
+
)
|
| 81 |
+
except SlackApiError as e:
|
| 82 |
+
response="Error"
|
| 83 |
+
return response
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
app.run(host="0.0.0.0", port=7860)
|