chatyte / app.py
nezahatkorkmaz's picture
Update app.py
02b7665 verified
raw
history blame
2.2 kB
import gradio as gr
from huggingface_hub import InferenceClient
import requests
from bs4 import BeautifulSoup
import json
from transformers import pipeline
import gradio as gr
# Veri çekme fonksiyonu
def fetch_tubitak_data():
"""
TÜBİTAK web sitesinden bilgi çeken fonksiyon.
"""
url = "https://www.tubitak.gov.tr/tr"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# TÜBİTAK haberlerini çekme
news = []
for item in soup.find_all("div", class_="news-title"):
title = item.text.strip()
link = item.find("a")["href"]
news.append({"title": title, "link": f"https://www.tubitak.gov.tr{link}"})
# Veriyi JSON dosyasına kaydetme
with open("tubitak_data.json", "w") as file:
json.dump(news, file)
return news
# Model yükleme
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
# Veri tabanından yanıt bulma
def get_answer_from_data(question, data_path="tubitak_data.json"):
"""
JSON dosyasından bilgi alarak yanıt oluşturan fonksiyon.
"""
with open(data_path, "r") as file:
data = json.load(file)
context = " ".join([f"{item['title']}: {item['description']}" for item in data])
# Soru-cevap modeli kullanarak yanıt oluşturma
result = qa_pipeline(question=question, context=context)
return result["answer"]
# Chatbot fonksiyonu
def chatbot(question):
"""
Kullanıcının TÜBİTAK ve BİLGEM hakkındaki sorularına https://yteblog.bilgem.tubitak.gov.tr/ ve https://tubitak.gov.tr/tr sayfalarıyla yanıt veren chatbot fonksiyonu.
"""
try:
# Güncel veri çekme (isteğe bağlı)
fetch_tubitak_data()
# Yanıt oluşturma
answer = get_answer_from_data(question)
return answer
except Exception as e:
return f"Hata oluştu: {str(e)}"
# Gradio arayüzü
demo = gr.Interface(
fn=chatbot,
inputs="text",
outputs="text",
title="TÜBİTAK Chatbot",
description="TÜBİTAK ve BİLGEM hakkında sorularınızı yanıtlar."
)
# Uygulama başlatma
if __name__ == "__main__":
demo.launch()