Update app.py
Browse files
app.py
CHANGED
@@ -6,41 +6,34 @@ import numpy as np
|
|
6 |
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
-
# β
Load
|
10 |
with open("texts.json", "r", encoding="utf-8") as f:
|
11 |
texts = json.load(f)
|
12 |
|
13 |
index = faiss.read_index("faiss_index.bin")
|
14 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
15 |
|
16 |
-
# β
|
17 |
-
API_KEY = os.environ.get("
|
18 |
-
MODEL = "
|
19 |
|
20 |
-
# β
|
21 |
def get_context(query, top_k=5):
|
22 |
query_vec = embed_model.encode([query])
|
23 |
D, I = index.search(np.array(query_vec), top_k)
|
24 |
-
|
25 |
-
return "\n".join(context_chunks).strip()
|
26 |
|
27 |
-
# β
Chat function
|
28 |
def chat_fn(message, history):
|
29 |
-
context = get_context(message)
|
30 |
-
|
31 |
-
if not context:
|
32 |
-
return "β οΈ Sorry, no relevant data found in the provided dataset."
|
33 |
-
|
34 |
headers = {
|
35 |
"Authorization": f"Bearer {API_KEY}",
|
36 |
"Content-Type": "application/json"
|
37 |
}
|
38 |
|
|
|
|
|
39 |
messages = [
|
40 |
-
{
|
41 |
-
"role": "system",
|
42 |
-
"content": "You are a helpful assistant. Use ONLY the provided context to answer. Do not use any external data.\n\nContext:\n" + context
|
43 |
-
}
|
44 |
]
|
45 |
|
46 |
for user, assistant in history:
|
@@ -51,13 +44,11 @@ def chat_fn(message, history):
|
|
51 |
|
52 |
payload = {
|
53 |
"model": MODEL,
|
54 |
-
"messages": messages
|
55 |
-
"temperature": 0.7,
|
56 |
-
"max_tokens": 512
|
57 |
}
|
58 |
|
59 |
try:
|
60 |
-
response = requests.post("https://
|
61 |
response.raise_for_status()
|
62 |
reply = response.json()["choices"][0]["message"]["content"]
|
63 |
except Exception as e:
|
@@ -65,13 +56,10 @@ def chat_fn(message, history):
|
|
65 |
|
66 |
return reply
|
67 |
|
68 |
-
# β
Gradio
|
69 |
-
|
70 |
fn=chat_fn,
|
71 |
-
title="",
|
72 |
-
description="",
|
73 |
-
|
74 |
-
|
75 |
-
)
|
76 |
-
|
77 |
-
chat_ui.launch(inbrowser=True, share=True)
|
|
|
6 |
import json
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
+
# β
Load RAG-related files
|
10 |
with open("texts.json", "r", encoding="utf-8") as f:
|
11 |
texts = json.load(f)
|
12 |
|
13 |
index = faiss.read_index("faiss_index.bin")
|
14 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
15 |
|
16 |
+
# β
Use your OpenRouter API key
|
17 |
+
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
18 |
+
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
19 |
|
20 |
+
# β
Function to search relevant context
|
21 |
def get_context(query, top_k=5):
|
22 |
query_vec = embed_model.encode([query])
|
23 |
D, I = index.search(np.array(query_vec), top_k)
|
24 |
+
return "\n".join([texts[i] for i in I[0]])
|
|
|
25 |
|
26 |
+
# β
Chat handler function
|
27 |
def chat_fn(message, history):
|
|
|
|
|
|
|
|
|
|
|
28 |
headers = {
|
29 |
"Authorization": f"Bearer {API_KEY}",
|
30 |
"Content-Type": "application/json"
|
31 |
}
|
32 |
|
33 |
+
context = get_context(message)
|
34 |
+
|
35 |
messages = [
|
36 |
+
{"role": "system", "content": "You are a helpful assistant. Use the following context to answer: " + context}
|
|
|
|
|
|
|
37 |
]
|
38 |
|
39 |
for user, assistant in history:
|
|
|
44 |
|
45 |
payload = {
|
46 |
"model": MODEL,
|
47 |
+
"messages": messages
|
|
|
|
|
48 |
}
|
49 |
|
50 |
try:
|
51 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
52 |
response.raise_for_status()
|
53 |
reply = response.json()["choices"][0]["message"]["content"]
|
54 |
except Exception as e:
|
|
|
56 |
|
57 |
return reply
|
58 |
|
59 |
+
# β
Launch Gradio ChatInterface
|
60 |
+
gr.ChatInterface(
|
61 |
fn=chat_fn,
|
62 |
+
title="CODEX MIRXA KAMRAN",
|
63 |
+
description="Chat with AI MODEL trained By Mirxa Kamran",
|
64 |
+
theme="soft"
|
65 |
+
).launch()
|
|
|
|
|
|