|
import gradio as gr |
|
import faiss |
|
import json |
|
import numpy as np |
|
import os |
|
from sentence_transformers import SentenceTransformer |
|
import requests |
|
|
|
|
|
embed_model = SentenceTransformer("all-MiniLM-L6-v2") |
|
|
|
|
|
index = faiss.read_index("faiss_index.bin") |
|
with open("texts.json", "r") as f: |
|
texts = json.load(f) |
|
|
|
|
|
API_KEY = os.environ.get("OPENROUTER_API_KEY") |
|
MODEL = "deepseek/deepseek-chat-v3-0324:free" |
|
|
|
|
|
def search_context(query, k=5): |
|
query_embedding = embed_model.encode([query]) |
|
distances, indices = index.search(np.array(query_embedding), k) |
|
relevant_chunks = [texts[i] for i in indices[0] if i < len(texts)] |
|
return "\n".join(relevant_chunks) |
|
|
|
def chat_fn(message, history): |
|
|
|
context = search_context(message) |
|
|
|
if not context.strip(): |
|
return "β Sorry, I can only answer questions related to the content of logiqcurve.com." |
|
|
|
|
|
messages = [ |
|
{"role": "system", "content": "You are an assistant that only answers based on the provided CONTEXT. Do not answer from general knowledge. If the answer is not in the CONTEXT, reply with 'Sorry, I can only answer questions related to the content of logiqcurve.com.'"}, |
|
{"role": "user", "content": f"CONTEXT:\n{context}\n\nQUESTION:\n{message}"}, |
|
] |
|
|
|
headers = { |
|
"Authorization": f"Bearer {API_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
payload = { |
|
"model": MODEL, |
|
"messages": messages |
|
} |
|
|
|
try: |
|
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload) |
|
response.raise_for_status() |
|
reply = response.json()["choices"][0]["message"]["content"] |
|
except Exception as e: |
|
reply = f"β Error: {e}" |
|
|
|
return reply |
|
|
|
|
|
chatbot = gr.ChatInterface( |
|
fn=chat_fn, |
|
title="LogiqCurve ChatBot", |
|
description="Ask anything about logiqcurve.com. This bot only answers based on its content.", |
|
theme="soft" |
|
) |
|
|
|
chatbot.launch() |
|
|