File size: 2,694 Bytes
ab773bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
"""
Basit Gradio Arayüzü — RAG Retrieval + Threshold Demo
------------------------------------------------------------
Bu arayüz, asıl ödev kapsamı olan retrieval+threshold mekanizmasını görsel
olarak göstermek içindir. Tam bir LLM-cevap-üretme sohbet botu değildir
(o adım src/search.py::format_answer_for_llm ile kolayca eklenebilir,
bkz. README "Sonraki Adım").

Çalıştırma:
    EMBEDDING_BACKEND=mock python app.py
"""
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import gradio as gr
from src import config
from src.search import RAGSearcher

searcher = RAGSearcher()


def answer_question(question):
    if not question or not question.strip():
        return "Lütfen bir soru yazın.", ""

    result = searcher.search(question)

    score_info = f"**En yüksek benzerlik skoru:** {result['top_score']:.3f}  (threshold: {searcher.threshold})"

    if not result["answered"]:
        return f"🚫 {result['message']}", score_info

    lines = ["✅ **İlgili doküman parçaları bulundu:**\n"]
    for i, chunk in enumerate(result["retrieved_chunks"], 1):
        lines.append(
            f"**{i}. [{chunk['title']}]({chunk['url']})** (skor: {chunk['score']:.3f})\n\n"
            f"> {chunk['chunk_text'][:400]}{'...' if len(chunk['chunk_text']) > 400 else ''}\n"
        )
    return "\n".join(lines), score_info


with gr.Blocks(title="Türkçe Tıbbi Doküman RAG Sistemi 🏥") as demo:
    gr.Markdown(
        f"""
        # 🏥 Türkçe Tıbbi Doküman Arama Sistemi (RAG)
        `umutertugrul/turkish-hospital-medical-articles` veri setinden chunk'lanmış
        makaleler üzerinde **eşik (threshold) kontrollü** semantik arama.
        Benzerlik skoru eşiğin altındaysa sistem **uydurma yapmaz**, doğrudan
        "cevap dokümanlarımda yok" der.

        Backend: `{config.EMBEDDING_BACKEND}` | Threshold: `{config.SIMILARITY_THRESHOLD}` |
        Embedding modeli: `{config.EMBEDDING_MODEL_ID if config.EMBEDDING_BACKEND == 'real' else 'mock (TF-IDF, offline test)'}`

        **Örnek pozitif soru:** "Tip 2 diyabetin belirtileri nelerdir?"
        **Örnek negatif soru:** "Bitcoin fiyatı bugün ne kadar?"
        """
    )
    question_box = gr.Textbox(label="Sorunuz", placeholder="Örn: Migren atağını ne tetikler?")
    submit_btn = gr.Button("Ara", variant="primary")
    score_output = gr.Markdown()
    answer_output = gr.Markdown()

    submit_btn.click(fn=answer_question, inputs=question_box, outputs=[answer_output, score_output])
    question_box.submit(fn=answer_question, inputs=question_box, outputs=[answer_output, score_output])

if __name__ == "__main__":
    demo.launch()