Spaces:
Running
Running
from fastapi import Request, FastAPI, BackgroundTasks | |
import gspread | |
import requests | |
import openai | |
# OpenAI API KEY | |
API_KEY = "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" | |
openai.api_key = API_KEY | |
# Chatbase API๋ฅผ ์ด์ฉํด ํ ์คํธ๋ฅผ ๋ณด๋ด๊ณ ์๋ต์ ๋ฐ๋ ํจ์ | |
def getResponseFromChatbase(prompt): | |
url = "https://www.chatbase.co/api/v1/chat" | |
payload = { | |
"stream": False, | |
"temperature": 0, | |
"chatId": "6dMpdT5zPPS5ik9pKptsH", | |
"messages": [{"role": "user", "content": prompt}] | |
} | |
headers = { | |
"accept": "application/json", | |
"content-type": "application/json", | |
"authorization": "Bearer e81b355c-30cc-47ca-b9fa-deb33f7159ad" | |
} | |
response = requests.post(url, json=payload, headers=headers) | |
# Chatbase API ์๋ต์ ํํ์ ๋ฐ๋ผ ์ ์ ํ ํ๋๋ฅผ ๋ฐํ | |
# {"text":"์๋ ํ์ธ์! ์ด๋๋ผ๋์ AI์ ๋๋ค. ์ด๋ค ๋์์ด ํ์ํ์ ๊ฐ์?"} | |
return response.json()['text'] | |
def getResponseFromGooey(prompt): | |
url = "https://api.gooey.ai/v2/TextToSpeech/" | |
gooey_key = 'sk-lbYNmTYKFA7Fb0yP60QsB5huMHjBmyRAJ0kJeFZMdgifeAEN' | |
payload = { | |
"text_prompt": prompt, | |
"tts_provider": "ELEVEN_LABS", | |
"uberduck_voice_name": "the-rock", | |
"uberduck_speaking_rate": 1, | |
"google_voice_name": "en-IN-Wavenet-A", | |
"google_speaking_rate": 1, | |
"google_pitch": 0, | |
"bark_history_prompt": "en_speaker_0", | |
"elevenlabs_voice_name": None, | |
"elevenlabs_api_key": "c5fb99a2b25402f104d246379bcf819a", | |
"elevenlabs_voice_id": "SKwm0HLYsVDCM2ruvw2p", | |
"elevenlabs_model": "eleven_multilingual_v2", | |
"elevenlabs_stability": 0.5, | |
"elevenlabs_similarity_boost": 0.75, | |
"elevenlabs_style": 0, | |
"elevenlabs_speaker_boost": True, | |
} | |
headers = { | |
"accept": "application/json", | |
"content-type": "application/json", | |
"authorization": "Bearer " + gooey_key | |
} | |
response = requests.post(url, json=payload, headers=headers) | |
return response.json()['output']['audio_url'] | |
# ๋ฉ์ธ์ง ์ ์ก์ ์ํ ํฌ๋งท ํจ์ | |
def textResponseFormatKakao(bot_response): | |
response = { | |
'version': '2.0', | |
'template': { | |
'outputs': [{"simpleText": {"text": bot_response}}], | |
'quickReplies': [] | |
} | |
} | |
return response | |
# ChatGPT์๊ฒ ์ง๋ฌธ/๋ต๋ณ ๋ฐ๊ธฐ | |
def getTextFromGPT(prompt): | |
text = 'You are a thoughtful assistant.'\ | |
' Respond to all input in 25 words and answer in korean.' | |
messages_prompt = [{ | |
"role": "system", | |
"content": text | |
}] | |
messages_prompt += [{"role": "user", "content": prompt}] | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", | |
messages=messages_prompt) | |
message = response["choices"][0]["message"]["content"] | |
return message | |
def create_callback_request_kakao(prompt, serverType, callbackUrl): | |
if serverType == 'CHAT': | |
# OpenAI API/Chatbase ๋ก prompt๋ฅผ ๋์ ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์์ต๋๋ค. | |
bot_res = getResponseFromChatbase(prompt) | |
elif serverType == 'AUDIO': | |
# Gooey๋ก prompt๋ฅผ ๋์ ธ์ ์ค๋์ค ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐ์์ต๋๋ค. | |
bot_res = getResponseFromGooey(prompt) | |
# callbackUrl๋ก ๊ฒฐ๊ณผ๊ฐ์ post๋ฐฉ์์ผ๋ก ์ ์กํฉ๋๋ค. | |
headers = {'Content-Type': 'application/json; charset=utf-8'} | |
requests.post( | |
callbackUrl, | |
json=textResponseFormatKakao(bot_res), | |
headers=headers, | |
timeout=5 | |
) | |
return "" | |
# FastAPI ์ ํ๋ฆฌ์ผ์ด์ ์ธ์คํด์ค ์์ฑ | |
app = FastAPI() | |
# ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฒ๋ฆฌ: verymuchmorethanastronomically.tistory.com/18 ์ฐธ๊ณ . | |
async def chat2(request: Request, background_tasks: BackgroundTasks,): | |
# await ์ฒ๋ฆฌํ์ง ์์ผ๋ฉด userRequest๋ฅผ ์ฝ๋ค๊ฐ ์๋ฌ๊ฐ ๋ฐ์๋ฉ๋๋ค. | |
kakao_request = await request.json() | |
# /ask, /img, /gs ์ฒ๋ฆฌ. | |
if '/gs' in kakao_request["userRequest"]["utterance"]: | |
question = kakao_request["userRequest"]["utterance"].replace("/gs", "") | |
# json ํ์ผ์ด ์์นํ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ผ๋ก ์ค์ผ ํฉ๋๋ค. | |
json_file_path = "botree-404708-b377977f1aa4.json" | |
gc = gspread.service_account(json_file_path) | |
sheet = 'https://docs.google.com/spreadsheets/d/'\ | |
'1Iq5N59xU725bmxas730B_UQYZvX4EK_0rnRL-egxl_I/edit?usp=sharing' | |
doc = gc.open_by_url(sheet) | |
worksheet = doc.worksheet("์ํธ1") | |
# ์ด์ ์ ์ ๋ ฅ๋ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ ๋ง์ง๋ง ํ์ ์ฐพ์์ ๋ค์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐ | |
last_row = len(worksheet.col_values(1)) + 1 | |
cell = f'A{last_row}' | |
# ์ฌ์ฉ์๊ฐ ์ฑ๋ด์ ์ ๋ ฅํ ๊ฐ์ ํด๋น ์ด์ ๋ฃ์ด์ค๋๋ค. | |
worksheet.update(cell, question) | |
return textResponseFormatKakao(f"{cell}์ ์์ฑ ์๋ฃ.") | |
elif '/sp' in kakao_request["userRequest"]["utterance"]: | |
prompt = kakao_request["userRequest"]["utterance"].replace("/sp", "") | |
type = 'AUDIO' | |
elif 'ask' in kakao_request['userRequest']['utterance']: | |
prompt = kakao_request["userRequest"]["utterance"].replace("/ask", "") | |
type = 'CHAT' | |
# ํ ์คํธ์ ๊ธธ์ด๊ฐ 10์ ์ด์ 200์ ์ดํ์ธ์ง ํ์ธ | |
length = len(kakao_request["userRequest"]["utterance"]) | |
if length < 5 or length > 200: | |
return textResponseFormatKakao("์ง๋ฌธ์ 5์ ์ด์ 200์ ์ดํ๋ก ์ ๋ ฅํด์ฃผ์ธ์") | |
# ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ์นด์นด์ค ์ฑ๋ด์๊ฒ ์๋ต์ ๋ณด๋ ๋๋ค. | |
background_tasks.add_task( | |
create_callback_request_kakao, | |
prompt=prompt, | |
serverType=type, | |
callbackUrl=kakao_request["userRequest"]["callbackUrl"], | |
) | |
# user_requests[user_id] += 1 | |
# useCallback์ true์ฒ๋ฆฌํด์ค์ผ ์นด์นด์ค์์ 1๋ถ๊ฐ callbackUrl์ ์ ํจํ์ํต๋๋ค. | |
return {"version": "2.0", "useCallback": True} | |