Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 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 |
-
Verilen içeriği belirtilen dosyaya kaydeder.
|
| 80 |
-
|
| 81 |
-
Args:
|
| 82 |
-
content (str): Dosyaya yazılacak metin içeriği.
|
| 83 |
-
filename (str): Çıktı dosyasının adı.
|
| 84 |
-
"""
|
| 85 |
-
try:
|
| 86 |
-
with open(filename, 'w', encoding='utf-8') as f:
|
| 87 |
-
f.write(content)
|
| 88 |
-
print(f"İçerik başarıyla '{filename}' dosyasına kaydedildi.")
|
| 89 |
-
except IOError as e:
|
| 90 |
-
print(f"Dosyaya yazma sırasında bir hata oluştu: {e}")
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# --- Programın Başlangıç Noktası ---
|
| 94 |
-
if __name__ == "__main__":
|
| 95 |
-
article_content = scrape_wikipedia_article(URL, HEADERS)
|
| 96 |
-
|
| 97 |
-
# scrape_wikipedia_article fonksiyonu bir içerik döndürdüyse (None değilse)
|
| 98 |
-
# bu içeriği dosyaya kaydet.
|
| 99 |
-
if article_content:
|
| 100 |
-
save_to_file(article_content, OUTPUT_FILENAME)
|
| 101 |
-
else:
|
| 102 |
-
print("İşlem başarısız oldu. Hiçbir içerik kaydedilmedi.")
|
| 103 |
-
|
|
|
|
| 1 |
+
# Gerekli kütüphaneleri içe aktarıyoruz.
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
|
| 6 |
+
# --- Çekirdek Mantık ---
|
| 7 |
+
HEADERS = {
|
| 8 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def scrape_wikipedia_article(url: str):
|
| 12 |
+
"""
|
| 13 |
+
Kullanıcının girdiği URL'den Wikipedia makalesini çeker ve işler.
|
| 14 |
+
Bu fonksiyon, Gradio arayüzünün "beyni" olarak çalışacaktır.
|
| 15 |
+
"""
|
| 16 |
+
if not url or "wikipedia.org" not in url:
|
| 17 |
+
return "Lütfen geçerli bir Wikipedia URL'si girin.", 0, ""
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
response = requests.get(url, headers=HEADERS, timeout=10)
|
| 21 |
+
response.raise_for_status()
|
| 22 |
+
|
| 23 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
| 24 |
+
|
| 25 |
+
page_title_element = soup.find('h1', id='firstHeading')
|
| 26 |
+
page_title = page_title_element.text if page_title_element else "Başlık Bulunamadı"
|
| 27 |
+
|
| 28 |
+
content_div = soup.find('div', class_='mw-parser-output')
|
| 29 |
+
if not content_div:
|
| 30 |
+
return f"'{page_title}' sayfasında ana içerik alanı bulunamadı.", 0, ""
|
| 31 |
+
|
| 32 |
+
paragraphs_html = content_div.find_all('p')
|
| 33 |
+
cleaned_paragraphs = [p.get_text(strip=True) for p in paragraphs_html if len(p.get_text(strip=True)) > 50]
|
| 34 |
+
paragraph_count = len(cleaned_paragraphs)
|
| 35 |
+
|
| 36 |
+
if paragraph_count == 0:
|
| 37 |
+
return f"'{page_title}' sayfasında yeterli uzunlukta paragraf bulunamadı.", 0, ""
|
| 38 |
+
|
| 39 |
+
markdown_output = "\n".join([f"- {p}\n" for p in cleaned_paragraphs])
|
| 40 |
+
|
| 41 |
+
return page_title, paragraph_count, markdown_output
|
| 42 |
+
|
| 43 |
+
except requests.exceptions.RequestException as e:
|
| 44 |
+
error_message = f"Ağ hatası: URL'ye ulaşılamadı. Lütfen adresi kontrol edin.\nDetay: {e}"
|
| 45 |
+
return "Hata", 0, error_message
|
| 46 |
+
except Exception as e:
|
| 47 |
+
error_message = f"Beklenmedik bir hata oluştu: {e}"
|
| 48 |
+
return "Hata", 0, error_message
|
| 49 |
+
|
| 50 |
+
# --- Gradio Arayüzü Tanımlaması ---
|
| 51 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
+
gr.Markdown(
|
| 53 |
+
"""
|
| 54 |
+
# Wikipedia Makale Çekme ve Analiz Aracı
|
| 55 |
+
Aşağıya bir Wikipedia makalesinin URL'sini yapıştırın ve "Analiz Et" butonuna tıklayarak
|
| 56 |
+
sayfadaki paragrafları çekin.
|
| 57 |
+
"""
|
| 58 |
+
)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
+
url_input = gr.Textbox(label="Wikipedia URL'si", placeholder="https://tr.wikipedia.org/wiki/Yapay_zeka")
|
| 62 |
+
submit_btn = gr.Button("Analiz Et", variant="primary")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
article_title_output = gr.Textbox(label="Makale Başlığı", interactive=False)
|
| 65 |
+
paragraph_count_output = gr.Number(label="Paragraf Sayısı", interactive=False)
|
| 66 |
+
with gr.Column(scale=2):
|
| 67 |
+
main_output = gr.Markdown(label="Çekilen Paragraflar")
|
| 68 |
+
|
| 69 |
+
submit_btn.click(
|
| 70 |
+
fn=scrape_wikipedia_article,
|
| 71 |
+
inputs=url_input,
|
| 72 |
+
outputs=[article_title_output, paragraph_count_output, main_output]
|
| 73 |
+
)
|
| 74 |
+
gr.Examples(
|
| 75 |
+
["https://tr.wikipedia.org/wiki/Kuantum_bilgisayar%C4%B1", "https://tr.wikipedia.org/wiki/Makine_%C3%B6%C4%9Frenmesi"],
|
| 76 |
+
inputs=url_input
|
| 77 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|