File size: 2,991 Bytes
536aa54
56c10f0
536aa54
 
 
 
 
 
56c10f0
 
536aa54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f47bc7
536aa54
 
 
8c510f9
 
 
bd6df30
536aa54
 
8c510f9
 
 
536aa54
bd6df30
c5c6c23
296a95f
536aa54
 
 
 
 
 
 
 
8c510f9
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
import numpy as np
import gradio as gr
import os
import openai
import newspaper
import json
import re
from transformers import GPT2Tokenizer


# define the text summarizer function
def flip_text(request, page_url, contraseña, temp):
    try:
        page = newspaper.Article(url=page_url)
        page.download()
        page.parse()
    except Exception as e:
        return "", f"--- An error occurred while processing the URL: {e} ---", ""

    tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

    tokens = tokenizer.tokenize(page.text)
    num_tokens = len(tokens)

    if num_tokens > 10 and num_tokens < 2000:
        openai.api_key = contraseña
        # get the response from openai API
        try:
            response = openai.Completion.create(
                engine="text-davinci-003",
                prompt=request + "\n\n" + page.text,
                max_tokens=2048,
                temperature=temp,
                top_p=0.9,
            )
            # get the response text
            response_text = response.choices[0].text
            # clean the response text
            response_text = re.sub(r'\s+', ' ', response_text)
            return page.text, response_text, num_tokens
        except Exception as e:
            return page.text, f"--- An error occurred while processing the request: {e} ---", num_tokens
    return page.text, "--- Max number of tokens ---", num_tokens


# define the gradio interface
iface = gr.Interface(
    fn=flip_text,
    inputs=[gr.Textbox(lines=1, placeholder="분석하라:", label="프롬프트:", type="text"),
            gr.Textbox(lines=1, placeholder="URL을 붙여 넣으세요: ", label="URL 붙여넣기:", type="text"),
            gr.Textbox(lines=1, placeholder="승인된 API-key를 입력하세요.", label="API-Key:", type="password"),
            gr.Slider(0.0,1.0, value=0.7, label="Temperature:")
            ],
    outputs=[gr.Textbox(label="Input:"), gr.Textbox(label="Output:"), gr.Textbox(label="Tokens:")],
    examples=[["기사를 요약하라:","http://korating.com/news/view.php?idx=7573","",0.7],
            ["한글로 번역하라:\n- 요약:\n- 비즈니스 영향도:\n- 투자 가치:", "https://ai.googleblog.com/2019/10/quantum-supremacy-using-programmable.html","",0.7],
            ["종목 분석 요약하라:","https://securities.miraeasset.com/bbs/board/message/view.do?messageId=2297456&messageNumber=492&messageCategoryId=0&startId=zzzzz%7E&startPage=1&curPage=1&searchType=2&searchText=&searchStartYear=2022&searchStartMonth=02&searchStartDay=01&searchEndYear=2023&searchEndMonth=02&searchEndDay=01&lastPageFlag=&vf_headerTitle=&categoryId=1545","",0.7]
    ],

    title="한글 및 다국어 + ChatGPT + 멀티 모델링 기반 AI",
    description="Voice 자동 분석: https://seawolf2357-aivora.hf.space \n "
)

error_message = ""

try:
    iface.launch()
except Exception as e:
    error_message = "An error occurred: " + str(e)
    iface.outputs[1].value = error_message