Spaces:
Runtime error
Runtime error
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 |