|
import gradio as gr |
|
import requests |
|
import json |
|
import os |
|
import faiss |
|
import numpy as np |
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
index = faiss.read_index("faiss_index.bin") |
|
with open("texts.json", "r") as f: |
|
texts = json.load(f) |
|
|
|
|
|
model = SentenceTransformer("all-MiniLM-L6-v2") |
|
|
|
|
|
API_KEY = os.environ.get("OPENROUTER_API_KEY") |
|
MODEL = "deepseek/deepseek-chat-v3-0324:free" |
|
|
|
|
|
def get_relevant_context(query, k=5): |
|
query_vector = model.encode([query]) |
|
scores, indices = index.search(np.array(query_vector), k) |
|
return [texts[i] for i in indices[0] if i < len(texts)] |
|
|
|
|
|
def chat_with_data(message, history): |
|
greetings = ["hi", "hello", "hey", "salam", "assalamualaikum", "good morning", "good evening"] |
|
message_lower = message.lower().strip() |
|
|
|
if any(greet in message_lower for greet in greetings): |
|
return "π Hello! How can I assist you regarding LogiqCurve today?" |
|
|
|
context = get_relevant_context(message) |
|
if not context: |
|
return "β Sorry, I can only answer questions related to LogiqCurve and its services." |
|
|
|
context_text = "\n".join(context) |
|
prompt = f"You are a helpful assistant for LogiqCurve. Use only the following context:\n\n{context_text}\n\nUser: {message}" |
|
|
|
messages = [ |
|
{"role": "system", "content": "You are a helpful assistant that answers only using provided context."}, |
|
{"role": "user", "content": prompt} |
|
] |
|
|
|
headers = { |
|
"Authorization": f"Bearer {API_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
payload = { |
|
"model": MODEL, |
|
"messages": messages |
|
} |
|
|
|
try: |
|
res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload) |
|
res.raise_for_status() |
|
return res.json()["choices"][0]["message"]["content"] |
|
except Exception as e: |
|
return f"β Error: {e}" |
|
|
|
|
|
gr.ChatInterface( |
|
fn=chat_with_data, |
|
title="MK Assistant", |
|
description="Ask questions related to LogiqCurve. Chat is limited to website-related content only.", |
|
theme="soft" |
|
).launch() |
|
|