File size: 2,377 Bytes
0d2e704
9e11cef
417cd16
9e11cef
 
 
a0b2f3c
4991a9b
a0b2f3c
33c1a1e
a0b2f3c
2036883
33c1a1e
417cd16
 
 
 
 
a0b2f3c
fada29a
78d5f9d
 
 
 
 
 
9e11cef
 
 
78d5f9d
 
 
 
 
9e11cef
4991a9b
 
88e5e9b
4991a9b
 
 
 
 
930de57
4991a9b
 
 
 
 
 
 
aec0035
4991a9b
 
fada29a
0d2e704
 
 
 
 
 
 
 
417cd16
ec8aa17
 
4991a9b
fada29a
 
b3fb86f
 
c317c89
 
b61760d
2acf559
 
fada29a
 
 
33c1a1e
 
 
de2dd13
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
78
79
80
81
82
83
84
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):
    if input_text:
        _prompt = f"""
        以下の文章を要約してください。
        {input_text}
        """
    elif 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}
        """
    else:
        return "テキストかURLを入力してください。"

    config = FilmConfig(
        "gpt-3.5-turbo-16k",
        api_type="azure",
        azure_deployment_id="gpt-35-turbo",
        azure_api_version="2023-05-15",
        timeout=60.0,  # これを入れないとtimeoutが頻繁に発生する
    )
    config.get_apikey()

    film = FilmCore(
        prompt=_prompt,
        system_prompt="あなたは優秀なライターです。",
        config=config
    )

    return await film.run_async()


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)