sahinomercan commited on
Commit
ef3ea72
·
verified ·
1 Parent(s): a2ea8df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -52
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
4
  import torch
5
  import os
6
  import pickle
@@ -10,38 +10,27 @@ import re
10
  TEXT_FILE = "icerik.txt"
11
  EMBEDDING_CACHE_FILE = "embeddings.pkl"
12
  EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2"
13
- GENERATOR_MODEL_NAME = "google/flan-t5-small"
14
- THRESHOLD = 0.55 # Minimum güven skoru (Retrieval için)
15
 
16
- # İLETİŞİM BİLGİLERİ (Güncel ve Entegre)
 
 
 
17
  PHONE_MAIN = '+90 531 294 22 34'
18
  PHONE_LANDLINE_EXT = '0232 464 41 00 (Dahili 165)'
19
  WHATSAPP_LINK = 'http://wa.me/905312942234'
20
  APPOINTMENT_LINK = 'https://calendar.app.google/JT9A1oGHVGopNZ9y8'
21
  MAP_LINK = 'https://maps.app.goo.gl/PLsBy9afjiRB9WDb6'
22
- EMAIL_ADDRESS = '[email protected]'
23
 
24
 
25
  # --- 2. MODELLERİN YÜKLENMESİ ve VERİ YÜKLEME ---
26
  print("Modeller yükleniyor...")
 
27
  embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
28
-
29
- # Generative Model (PyTorch) Yükleniyor
30
- try:
31
- generator_tokenizer = AutoTokenizer.from_pretrained(GENERATOR_MODEL_NAME)
32
- generator_model = AutoModelForSeq2SeqLM.from_pretrained(
33
- GENERATOR_MODEL_NAME,
34
- device_map="auto" # CPU'ya yüklenmesini sağlar
35
- )
36
- generator_pipeline = pipeline(
37
- "text2text-generation",
38
- model=generator_model,
39
- tokenizer=generator_tokenizer
40
- )
41
- print("PyTorch Generative AI modeli yüklendi.")
42
- except Exception as e:
43
- print(f"Generative AI YÜKLEME HATASI: {e}")
44
- generator_pipeline = None
45
 
46
 
47
  def load_documents():
@@ -66,9 +55,8 @@ def load_documents():
66
 
67
  docs, embeddings = load_documents()
68
 
69
- # --- 3. POST-PROCESSİNG ve UTILS ---
70
  def post_process_context(context: str) -> str:
71
- """Metin temizliği yapar ve düzenli bir görünüm için HTML'e dönüştürür."""
72
  paragraphs = context.split('\n\n')
73
  cleaned_paragraphs = []
74
  for p in paragraphs:
@@ -79,15 +67,15 @@ def post_process_context(context: str) -> str:
79
  return "<br> • " + "<br> • ".join(cleaned_paragraphs)
80
 
81
 
82
- # --- 4. ANA MANTIK (Generative RAG) ---
83
 
84
  def answer_question(question: str):
85
 
86
- if generator_pipeline is None or not docs or embeddings is None:
87
- return "<div style='font-family: Arial; color: #8b0000;'><h3>Sistem Hatası:</h3><p>Lokal bilgi kaynakları veya Yapay Zeka motoru yüklenemedi. Lütfen 'icerik.txt' dosyasını kontrol edin ve tekrar deneyin.</p></div>"
88
 
89
 
90
- # 1. Retrieval (Çekme)
91
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
92
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
93
  top_k = min(3, len(docs))
@@ -95,7 +83,7 @@ def answer_question(question: str):
95
 
96
  selected_context = []
97
  for score, idx in zip(top_results.values, top_results.indices):
98
- if score.item() >= THRESHOLD:
99
  selected_context.append(docs[idx.item()])
100
 
101
  if not selected_context:
@@ -111,25 +99,23 @@ def answer_question(question: str):
111
 
112
  full_context = "\n\n".join(selected_context)
113
 
114
- # 2. Generation (Üretme)
115
- rag_prompt = f"""
116
- Sana sunulan metni (Bağlam) kullanarak, aşağıdaki soruyu SADECE TÜRKÇE ve mantıklı cümlelerle özetle. Cevabın 2-3 cümleyi geçmesin ve sadece bağlamdaki bilgilere dayanmalıdır. Eğer bağlamda kesin cevap yoksa 'Net bir cevap bulunamadı.' de.
117
-
118
- Soru: {question}
119
 
120
- Bağlam: {full_context}
 
121
 
122
- Özet Cevap:
123
- """
124
-
125
- generated_output = generator_pipeline(
126
- rag_prompt,
127
- max_new_tokens=100,
128
- do_sample=False,
129
- temperature=0.01
130
- )
131
-
132
- synthesized_answer = generated_output[0]['generated_text'].strip()
133
 
134
  # 3. Final Sunum
135
  processed_context_html = post_process_context(full_context)
@@ -139,19 +125,19 @@ def answer_question(question: str):
139
  <h3 style="color: #003366;">🧾 Sorunuz</h3>
140
  <p><strong>{question}</strong></p>
141
 
142
- <h3 style="color: #003366;">✅ Ön Rehberlik ve Yol Haritası</h3>
143
  <p style="background-color: #eef7ff; border-left: 5px solid #005580; padding: 15px; border-radius: 5px; font-weight: bold;">
144
- {synthesized_answer}
145
  </p>
146
 
147
- <h3 style="color: #003366; margin-top: 20px;">📚 Kaynak Metin (Analiz Özeti)</h3>
148
  <p style="font-size: 14px; color: #333; padding: 0 15px 0 15px;">
149
  {processed_context_html}
150
  </p>
151
 
152
  <h3 style="color: #8b0000; margin-top: 25px;">📢 Kişisel Değerlendirme ve Görüşme</h3>
153
  <p style="font-size: 16px;">
154
- Bu özet bilgi, genel durumunuz hakkında bir fikir verir. Detaylı durum tespiti ve kişisel yol haritanız için hemen bizimle iletişime geçin:
155
  <br><br>
156
  <a href='{WHATSAPP_LINK}' style='color: #003366; font-weight: bold; text-decoration: underline;'>📞 WhatsApp Üzerinden Hızlı İletişim</a>
157
  <br>
@@ -162,7 +148,7 @@ def answer_question(question: str):
162
  return answer_html
163
 
164
  # --- 5. GRADIO ARAYÜZÜ ---
165
- # Tüm iletişim bilgileri ve Randevu/Harita linkleri buraya entegre edildi.
166
  header_info = """
167
  <div style="font-family: Arial; padding: 15px; background: linear-gradient(to right, #003366, #005580); color: white; border-radius: 10px;">
168
  <h2 style="color: white;">Şahin Hukuk | Akıllı Asistan</h2>
@@ -181,7 +167,6 @@ header_info = """
181
  MAP_LINK=MAP_LINK
182
  )
183
 
184
-
185
  interface = gr.Interface(
186
  fn=answer_question,
187
  inputs=gr.Textbox(label="Sorunuzu buraya yazın", lines=2, placeholder="Örn: Kira tespit davası nedir?"),
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
+ from transformers import pipeline # T5 yerine standart pipeline kullanacağız
4
  import torch
5
  import os
6
  import pickle
 
10
  TEXT_FILE = "icerik.txt"
11
  EMBEDDING_CACHE_FILE = "embeddings.pkl"
12
  EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2"
13
+ # YENİ MODEL: Türkçe için eğitilmiş, "saçmalamayan" Extractive QA modeli
14
+ QA_MODEL_NAME = "savasy/bert-base-turkish-squad"
15
 
16
+ RETRIEVAL_THRESHOLD = 0.55 # Paragraf bulma benzerlik skoru
17
+ QA_THRESHOLD = 0.40 # Cevap çıkarma güven skoru
18
+
19
+ # İLETİŞİM BİLGİLERİ (Arayüze Entegre Edilecek)
20
  PHONE_MAIN = '+90 531 294 22 34'
21
  PHONE_LANDLINE_EXT = '0232 464 41 00 (Dahili 165)'
22
  WHATSAPP_LINK = 'http://wa.me/905312942234'
23
  APPOINTMENT_LINK = 'https://calendar.app.google/JT9A1oGHVGopNZ9y8'
24
  MAP_LINK = 'https://maps.app.goo.gl/PLsBy9afjiRB9WDb6'
 
25
 
26
 
27
  # --- 2. MODELLERİN YÜKLENMESİ ve VERİ YÜKLEME ---
28
  print("Modeller yükleniyor...")
29
+ # 1. Paragraf Bulucu (Retrieval)
30
  embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
31
+ # 2. Cevap Çıkarıcı (Extraction)
32
+ qa_pipeline = pipeline("question-answering", model=QA_MODEL_NAME, tokenizer=QA_MODEL_NAME)
33
+ print("Tüm modeller yüklendi.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
36
  def load_documents():
 
55
 
56
  docs, embeddings = load_documents()
57
 
58
+ # --- 3. POST-PROCESSİNG (Sadece Kaynak Gösterimi İçin) ---
59
  def post_process_context(context: str) -> str:
 
60
  paragraphs = context.split('\n\n')
61
  cleaned_paragraphs = []
62
  for p in paragraphs:
 
67
  return "<br> • " + "<br> • ".join(cleaned_paragraphs)
68
 
69
 
70
+ # --- 4. ANA MANTIK (Extractive RAG) ---
71
 
72
  def answer_question(question: str):
73
 
74
+ if qa_pipeline is None or not docs or embeddings is None:
75
+ return "<div style='font-family: Arial; color: #8b0000;'><h3>Sistem Hatası:</h3><p>Lokal bilgi kaynakları veya Yapay Zeka motoru yüklenemedi.</p></div>"
76
 
77
 
78
+ # 1. Retrieval (Çekme): En alakalı paragrafları bul
79
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
80
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
81
  top_k = min(3, len(docs))
 
83
 
84
  selected_context = []
85
  for score, idx in zip(top_results.values, top_results.indices):
86
+ if score.item() >= RETRIEVAL_THRESHOLD:
87
  selected_context.append(docs[idx.item()])
88
 
89
  if not selected_context:
 
99
 
100
  full_context = "\n\n".join(selected_context)
101
 
102
+ # 2. Extraction (Cevabı Çıkarma) - YENİ KISIM
103
+ # Model, bu metin içinden sorunun cevabını bulur
104
+ qa_result = qa_pipeline(question=question, context=full_context)
 
 
105
 
106
+ answer = qa_result['answer']
107
+ score = qa_result['score']
108
 
109
+ # Güven skoru düşükse (cevap alakasızsa) veya cevap çok kısaysa
110
+ if score < QA_THRESHOLD or len(answer) < 10:
111
+ return f"""
112
+ <div style="font-family: Arial; color: #8b0000;">
113
+ <h3>Bilgi bankamızda bu soruya yönelik net bir cevap bulunamadı.</h3>
114
+ <p>Lütfen ofisimizle iletişime geçin.
115
+ <a href='{WHATSAPP_LINK}' style='color: #8b0000; text-decoration: underline;'>WhatsApp üzerinden bize ulaşabilirsiniz</a>.
116
+ </p>
117
+ </div>
118
+ """
 
119
 
120
  # 3. Final Sunum
121
  processed_context_html = post_process_context(full_context)
 
125
  <h3 style="color: #003366;">🧾 Sorunuz</h3>
126
  <p><strong>{question}</strong></p>
127
 
128
+ <h3 style="color: #003366;">✅ Yapay Zeka Yanıtı (Çıkarım)</h3>
129
  <p style="background-color: #eef7ff; border-left: 5px solid #005580; padding: 15px; border-radius: 5px; font-weight: bold;">
130
+ {answer}
131
  </p>
132
 
133
+ <h3 style="color: #003366; margin-top: 20px;">📚 Cevabın Alındığı Kaynak Metinler</h3>
134
  <p style="font-size: 14px; color: #333; padding: 0 15px 0 15px;">
135
  {processed_context_html}
136
  </p>
137
 
138
  <h3 style="color: #8b0000; margin-top: 25px;">📢 Kişisel Değerlendirme ve Görüşme</h3>
139
  <p style="font-size: 16px;">
140
+ Detaylı durum tespiti ve kişisel yol haritanız için hemen bizimle iletişime geçin:
141
  <br><br>
142
  <a href='{WHATSAPP_LINK}' style='color: #003366; font-weight: bold; text-decoration: underline;'>📞 WhatsApp Üzerinden Hızlı İletişim</a>
143
  <br>
 
148
  return answer_html
149
 
150
  # --- 5. GRADIO ARAYÜZÜ ---
151
+ # (Arayüz kodunda değişiklik yok, tüm iletişim bilgileri yerinde)
152
  header_info = """
153
  <div style="font-family: Arial; padding: 15px; background: linear-gradient(to right, #003366, #005580); color: white; border-radius: 10px;">
154
  <h2 style="color: white;">Şahin Hukuk | Akıllı Asistan</h2>
 
167
  MAP_LINK=MAP_LINK
168
  )
169
 
 
170
  interface = gr.Interface(
171
  fn=answer_question,
172
  inputs=gr.Textbox(label="Sorunuzu buraya yazın", lines=2, placeholder="Örn: Kira tespit davası nedir?"),