Spaces:
Runtime error
Runtime error
from flask import Flask, jsonify, render_template, request, send_file | |
from slack_sdk import WebClient | |
from slack_sdk.errors import SlackApiError | |
import os | |
from slack_bolt import App | |
import flask | |
import requests | |
import json | |
app = flask.Flask(__name__, template_folder="static") | |
def text_generate(prompt): | |
url = "http://34.122.217.42/complete_batch" | |
data = { | |
"debug_ext_path": "foo", | |
"context":prompt, | |
"top_p": 0.9, | |
"temp": 0.75} | |
try: | |
response = requests.post(url, json=data) | |
if response.status_code == 200: | |
data_str=response.json() | |
slack_msg="Supplied Prompt was :"+data["context"]+"\n\n" | |
slack_msg = slack_msg+"Response receievd was : \n\n" | |
for i in range(4): | |
slack_msg = slack_msg+data_str["completions"][i]["completion"]+"\n\n" | |
slack_msg = slack_msg+data_str["completions"][i]["log_prob"]+"\n\n" | |
else: | |
slack_msg="Failed to send JSON message to URL"+response.status_code | |
except: | |
slack_msg="Failed to send JSON message to URL"+response.status_code | |
return slack_msg | |
def index(): | |
return 'Hello' | |
def handle_event(): | |
# Handle the event here | |
event=request.json['type'] | |
if event=='url_verification': | |
# Respond to the challenge request with a 200 OK HTTP status code | |
# and the value of the challenge parameter in the response body | |
challenge = request.json['challenge'] | |
return ( | |
challenge | |
) | |
if event=='app_mention': | |
API_KEY=os.environ.get('SLACK_APP_TOKEN') | |
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL') | |
# Set up the Slack client | |
YOUR_BOT_TOKEN=API_KEY | |
client = WebClient(token=YOUR_BOT_TOKEN) | |
channel= SLACK_CHANNEL | |
conversation = request.json['text'] | |
print(conversation) | |
response=text_generate(conversation) | |
# Post the response back to the Slack channel | |
try: | |
client.chat_postMessage( | |
channel='#chat-gpt-bot', | |
text=response | |
) | |
except SlackApiError as e: | |
response="Error" | |
return response | |
if event=='event_callback': | |
API_KEY=os.environ.get('SLACK_APP_TOKEN') | |
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL') | |
# Set up the Slack client | |
YOUR_BOT_TOKEN=API_KEY | |
client = WebClient(token=YOUR_BOT_TOKEN) | |
channel= SLACK_CHANNEL | |
conversation = request.json['event'] ['text'] | |
print(conversation) | |
response=text_generate(conversation) | |
# Post the response back to the Slack channel | |
try: | |
client.chat_postMessage( | |
channel=channel, | |
text=response | |
) | |
except SlackApiError as e: | |
response="Error" | |
return response | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=7860) |