Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
import faiss
|
3 |
-
import numpy as np
|
4 |
import json
|
5 |
-
|
6 |
import os
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
# Load
|
|
|
9 |
with open("texts.json", "r") as f:
|
10 |
texts = json.load(f)
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
query_emb = embed_model.encode([query])
|
20 |
-
D, I = index.search(np.array(query_emb), k)
|
21 |
-
return [texts[i] for i in I[0] if i < len(texts)]
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
context_chunks = retrieve_chunks(message)
|
29 |
-
context = "\n".join(context_chunks)
|
30 |
-
if not context.strip():
|
31 |
-
return "β I'm sorry, I can only assist with questions related to LogiqCurve.com."
|
32 |
-
prompt = f"Answer based ONLY on this content:\n{context}\n\nQuestion: {message}"
|
33 |
-
else:
|
34 |
-
# Public fallback (general AI)
|
35 |
-
prompt = message
|
36 |
|
37 |
-
# Use OpenRouter API
|
38 |
headers = {
|
39 |
-
"Authorization": f"Bearer {
|
40 |
"Content-Type": "application/json"
|
41 |
}
|
42 |
|
43 |
payload = {
|
44 |
-
"model":
|
45 |
-
"messages":
|
46 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
47 |
-
{"role": "user", "content": prompt}
|
48 |
-
]
|
49 |
}
|
50 |
|
51 |
try:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
return res.json()["choices"][0]["message"]["content"]
|
56 |
except Exception as e:
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
# Gradio
|
60 |
-
|
61 |
-
fn=
|
62 |
-
title="LogiqCurve
|
63 |
-
description="
|
64 |
theme="soft"
|
65 |
)
|
66 |
|
67 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import faiss
|
|
|
3 |
import json
|
4 |
+
import numpy as np
|
5 |
import os
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
import requests
|
8 |
+
|
9 |
+
# Load embedding model
|
10 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
11 |
|
12 |
+
# Load FAISS index and texts
|
13 |
+
index = faiss.read_index("faiss_index.bin")
|
14 |
with open("texts.json", "r") as f:
|
15 |
texts = json.load(f)
|
16 |
|
17 |
+
# OpenRouter API Key (Set as secret in Hugging Face Space settings)
|
18 |
+
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
19 |
+
MODEL = "deepseek/deepseek-chat-v3-0324:free"
|
20 |
|
21 |
+
# Perform semantic search using FAISS
|
22 |
+
def search_context(query, k=5):
|
23 |
+
query_embedding = embed_model.encode([query])
|
24 |
+
distances, indices = index.search(np.array(query_embedding), k)
|
25 |
+
relevant_chunks = [texts[i] for i in indices[0] if i < len(texts)]
|
26 |
+
return "\n".join(relevant_chunks)
|
27 |
+
|
28 |
+
def chat_fn(message, history):
|
29 |
+
# Search knowledge base
|
30 |
+
context = search_context(message)
|
31 |
|
32 |
+
if not context.strip():
|
33 |
+
return "β Sorry, I can only answer questions related to the content of logiqcurve.com."
|
|
|
|
|
|
|
34 |
|
35 |
+
# Construct prompt
|
36 |
+
messages = [
|
37 |
+
{"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.'"},
|
38 |
+
{"role": "user", "content": f"CONTEXT:\n{context}\n\nQUESTION:\n{message}"},
|
39 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
headers = {
|
42 |
+
"Authorization": f"Bearer {API_KEY}",
|
43 |
"Content-Type": "application/json"
|
44 |
}
|
45 |
|
46 |
payload = {
|
47 |
+
"model": MODEL,
|
48 |
+
"messages": messages
|
|
|
|
|
|
|
49 |
}
|
50 |
|
51 |
try:
|
52 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
53 |
+
response.raise_for_status()
|
54 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
|
|
55 |
except Exception as e:
|
56 |
+
reply = f"β Error: {e}"
|
57 |
+
|
58 |
+
return reply
|
59 |
|
60 |
+
# Gradio Chat Interface (for Hugging Face Space)
|
61 |
+
chatbot = gr.ChatInterface(
|
62 |
+
fn=chat_fn,
|
63 |
+
title="LogiqCurve ChatBot",
|
64 |
+
description="Ask anything about logiqcurve.com. This bot only answers based on its content.",
|
65 |
theme="soft"
|
66 |
)
|
67 |
|
68 |
+
chatbot.launch()
|