Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app copy.py +54 -0
- kb_texts.pkl +3 -0
- rag.zip +3 -0
app copy.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
+
import faiss
|
| 5 |
+
import pickle
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
|
| 8 |
+
# API anahtarı (Hugging Face Secrets'ten otomatik alınır)
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
# FAISS index ve knowledge base yükleme
|
| 12 |
+
try:
|
| 13 |
+
index = faiss.read_index("kb_faiss.index")
|
| 14 |
+
with open("kb_texts.pkl", "rb") as f:
|
| 15 |
+
kb_texts = pickle.load(f)
|
| 16 |
+
except:
|
| 17 |
+
index = None
|
| 18 |
+
kb_texts = ["Bilgi tabanı yüklenemedi."]
|
| 19 |
+
|
| 20 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 21 |
+
|
| 22 |
+
# RAG + LLM fonksiyonu
|
| 23 |
+
def chat(query):
|
| 24 |
+
if index is not None:
|
| 25 |
+
q_emb = embedder.encode([query], convert_to_numpy=True)
|
| 26 |
+
D, I = index.search(q_emb, 1)
|
| 27 |
+
context = kb_texts[I[0][0]]
|
| 28 |
+
else:
|
| 29 |
+
context = "Knowledge base not found."
|
| 30 |
+
|
| 31 |
+
prompt = f"""
|
| 32 |
+
You are a helpful assistant for a Credit Card Fraud Detection project.
|
| 33 |
+
Context: {context}
|
| 34 |
+
Question: {query}
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
response = openai.ChatCompletion.create(
|
| 39 |
+
model="gpt-4o-mini",
|
| 40 |
+
messages=[{"role": "user", "content": prompt}],
|
| 41 |
+
max_tokens=300,
|
| 42 |
+
temperature=0
|
| 43 |
+
)
|
| 44 |
+
return response.choices[0].message.content
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"API error: {e}"
|
| 47 |
+
|
| 48 |
+
# Gradio arayüzü
|
| 49 |
+
demo = gr.Interface(fn=chat, inputs="text", outputs="text",
|
| 50 |
+
title="Fraud Detection Chatbot",
|
| 51 |
+
description="RAG destekli dolandırıcılık tespiti yardımcısı.")
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
kb_texts.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5cec1ffe7d90c9c2f7e5a9cf7b706f27ca2bb7a3e2b26fa5c66a3eeca9dfbc5d
|
| 3 |
+
size 807
|
rag.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4b331fa64dc9482df530c664111e42b074bfc22f3ed7dcf668e55ab10939a8a
|
| 3 |
+
size 106970075
|