multi_test / app.py
seawolf2357's picture
initial commit
296a95f
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