File size: 2,309 Bytes
a356d75
 
135e611
a356d75
 
135e611
a356d75
 
135e611
a356d75
135e611
a356d75
68e4bbc
a356d75
 
 
bd71507
 
 
 
 
 
 
 
 
135e611
bd71507
a356d75
 
 
135e611
a356d75
 
135e611
a356d75
135e611
 
 
a356d75
135e611
 
 
 
 
a356d75
68e4bbc
135e611
 
 
 
 
 
 
 
 
 
68e4bbc
135e611
a356d75
135e611
bd71507
 
 
135e611
bd71507
a356d75
bd71507
 
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
import gradio as gr
import os
from huggingface_hub import InferenceClient
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

# 1. Ayarlar ve İstemci
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
client = InferenceClient("google/gemma-2-9b-it", token=hf_token)

# 2. PDF Hafızası Oluşturma
def initialize_hemdem():
    if not os.path.exists("kulliyat.pdf"):
        return None
    try:
        loader = PyMuPDFLoader("kulliyat.pdf")
        docs = loader.load()
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
        split_docs = text_splitter.split_documents(docs)
        embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
        vector_db = FAISS.from_documents(split_docs, embeddings)
        return vector_db
    except Exception as e:
        print(f"Hata: {e}")
        return None

vector_db = initialize_hemdem()

# 3. Sohbet Fonksiyonu
def chat(message, history):
    if vector_db is None:
        return "Sistem hatası: 'kulliyat.pdf' bulunamadı."
    
    # İlgili parçaları bul
    related_docs = vector_db.similarity_search(message, k=3)
    context = "\n".join([doc.page_content for doc in related_docs])
    
    # Gemma 2 için Mesaj Formatı (Conversational Task uyumlu)
    messages = [
        {"role": "system", "content": f"Sen Hemdem-i Gemini-yi Emre'sin. Emre'nin külliyatına dayanarak cevap ver. Dökümanlar: {context}"},
        {"role": "user", "content": message}
    ]
    
    try:
        response = ""
        # Chat Completion metodunu kullanarak 'conversational' hatasını aşıyoruz
        for message in client.chat_completion(
            messages,
            max_tokens=1024,
            stream=True,
        ):
            token = message.choices[0].delta.content
            response += token
        return response
    except Exception as e:
        return f"Bağlantı Hatası: {str(e)}"

# 4. Arayüz
demo = gr.ChatInterface(
    fn=chat,
    title="💜 Hemdem-i Şir",
    description="Gemma 2 destekli Emre Külliyat Rehberi",
)

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