Spaces:
Runtime error
Runtime error
import requests | |
import gradio as gr | |
from bs4 import BeautifulSoup | |
from rich import print | |
from urllib.parse import urlparse | |
import re | |
from googlesearch import search | |
from urllib.parse import parse_qs | |
def run_lora(prompt,site,start,end): | |
#url = f"https://www.googleapis.com/customsearch/v1?q=site:{site}+{prompt}&cx={SEARCH_ENGINE_ID}&key=AIzaSyCnsm9J-9zkLTKLHrt7UiMXNuOU5ddas08&sort=date:r:{start}:{end}" | |
# API key từ NewsAPI | |
api_key = f'8ac7c497c32a43f09fe9c39a7a316239' | |
# URL API của NewsAPI | |
url = (f'https://newsapi.org/v2/everything?' | |
f'q={prompt}&' | |
f'domains={site}&' | |
f'from={start}&to={end}&' | |
f'sortBy=relevancy&' | |
f'apiKey=8ac7c497c32a43f09fe9c39a7a316239') | |
# Gửi yêu cầu GET tới NewsAPI | |
response = requests.get(url) | |
data = response.json() | |
links="" | |
if data["status"] == "ok": | |
for article in data["articles"]: # Lấy 5 bài báo đầu tiên | |
print(f"Title: {article['title']}") | |
print(f"Link: {article['url']}") | |
print(f"Published At: {article['publishedAt']}") | |
print('-' * 80) | |
links+="/n"+{article['url']} | |
else: | |
print("Error fetching data.") | |
return links | |
def extract_href(href): | |
url = urlparse(href) | |
query = parse_qs(url.query) | |
if not ('q' in query and query['q'] and len(query['q']) > 0): | |
return None | |
return query['q'][0] | |
def make_url(query,start,end): | |
return f"https://www.google.com/search?q={query}&rlz=1C1CHBF_enUS1024US1025&biw=1564&bih=932&sxsrf=ALiCzsaGPneyPAo-kyllnxBBtXe-FGWorQ%3A1665448856808&source=lnt&tbs=sbd%3A1%2Ccdr%3A1%2Ccd_min%3A{start}%2Ccd_max%3A{end}&tbm=nws" | |
with gr.Blocks() as app: | |
gr.HTML("""<html> | |
<head> | |
<style> | |
h1 { | |
text-align: center; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Get survey</h1> | |
</body> | |
</html>""") | |
with gr.Row(): | |
with gr.Column(): | |
input_textbox = gr.Textbox(lines=5, placeholder="Enter key", label="Input Text") | |
input_sitebox = gr.Textbox(lines=5, placeholder="Enter site", label="Site Text") | |
start = gr.Textbox(lines=5, placeholder="Enter start", label="Enter start") | |
end = gr.Textbox(lines=5, placeholder="Enter End", label="Enter End") | |
with gr.Column(): | |
translated_textbox = gr.Textbox(lines=5, placeholder="", label="Result Text") | |
info_label = gr.HTML("") | |
btn = gr.Button("GetNow") | |
btn.click(run_lora, inputs=[input_textbox,input_sitebox,start,end],outputs=[translated_textbox]) | |
app.queue() | |
app.launch() |