Spaces:
Runtime error
Runtime error
File size: 2,726 Bytes
23d19a2 5e90465 23d19a2 5e90465 9baddc4 a92f090 5e90465 b0717d6 5e90465 b0717d6 5e90465 a92f090 d472fa4 d085a03 a92f090 23d19a2 5e90465 23d19a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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")
#API_URL = "http://34.122.217.42/complete_batch"
API_URL = "https://api-inference.huggingface.co/models/Salesforce/codegen-350M-multi"
API_TOKEN = os.environ["API_TOKEN"]
headers = {"Authorization": f"Bearer {API_TOKEN}"}
def text_generate(prompt):
#Prints to debug the code
print(f"*****Inside text_generate - Prompt is :{prompt}")
data ={"inputs":prompt,
"top_p": 0.9,
"temp": 0.75}
try:
response = requests.post(API_URL, headers=headers, json=data)
print(response.json())
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"
'''
slack_msg=slack_msg+json.dumps(data_str)
else:
slack_msg="Failed to send JSON message to URL"
except requests.exceptions.Timeout:
slack_msg="Failed to send JSON message to URL due to timeout error"
pass
return slack_msg
@app.route("/")
def index():
return 'Hello'
@app.route("/events", methods=["POST"])
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=='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[15:])
# 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) |