Spaces:
Sleeping
Sleeping
import json | |
import os | |
import re | |
import requests | |
from bs4 import BeautifulSoup | |
import gradio as gr | |
from spiralfilm import FilmCore, FilmConfig | |
def greet(name): | |
return "こんにちは " + name + "さん!! \n僕はパスカルくんだよ。よろしくね" | |
def extract_texts(input_str): | |
pattern = r"msg='([^']*)'" | |
matches = re.findall(pattern, input_str) | |
return matches | |
async def summarize(input_text: str, input_url: str): | |
config = FilmConfig( | |
"gpt-4-32k", | |
api_type="azure", | |
azure_deployment_id="gpt-4-32k", | |
azure_api_version="2023-05-15", | |
timeout=60.0, # これを入れないとtimeoutが頻繁に発生する | |
) | |
config.get_apikey() | |
if input_text: | |
_prompt = f""" | |
以下の文章を要約してください。 | |
{input_text} | |
""" | |
return await FilmCore( | |
prompt=_prompt, | |
system_prompt="あなたは優秀なライターです。", | |
config=config | |
).run_async() | |
if input_url: | |
res = requests.get(input_url) | |
soup = BeautifulSoup(res.text) | |
url_content = soup.find('title').text + '\n' + soup.find('body').text | |
_prompt = f""" | |
以下の文章を要約してください。 | |
{url_content} | |
""" | |
res = await FilmCore( | |
prompt=_prompt, | |
system_prompt="あなたは優秀なライターです。", | |
config=config | |
).run_async() | |
return res + '\n' + input_url | |
else: | |
return "テキストかURLを入力してください。" | |
async def chat(input_text, input_url): | |
summary = await summarize(input_text, input_url) | |
endpoint = os.environ.get("TWINROOM_API_BASE") | |
payload = { | |
"content": summary | |
} | |
headers = {'API-Key': os.environ.get("TWINROOM_API_KEY")} | |
json_payload = json.dumps(payload) | |
response = requests.post(endpoint, headers=headers, data=json_payload) | |
response_msgs = extract_texts(response.text) | |
result = ''.join(response_msgs) | |
return result | |
with gr.Blocks() as iface: | |
# UI | |
with gr.Row(): | |
with gr.Column(): | |
input_text = gr.Textbox(label="テキスト") | |
input_url = gr.Textbox(label="URL") | |
chat_btn = gr.Button("Chat") | |
with gr.Column(): | |
output_text = gr.Textbox(label="回答") | |
# Event handler | |
chat_btn.click(fn=chat, inputs=[input_text, input_url], outputs=output_text) | |
if __name__ == "__main__": | |
iface.launch(auth=("spiralai", "spiralai"), share=True) | |