mirxakamran893's picture
Update app.py
c39c8ea verified
raw
history blame
1.92 kB
import gradio as gr
import requests
import os
import faiss
import numpy as np
import json
from sentence_transformers import SentenceTransformer
# βœ… Load context data
with open("texts.json", "r", encoding="utf-8") as f:
texts = json.load(f)
index = faiss.read_index("faiss_index.bin")
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
API_KEY = os.environ.get("OPENROUTER_API_KEY")
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
# βœ… Get top-k context from FAISS
def get_context(query, top_k=5):
query_vec = embed_model.encode([query])
D, I = index.search(np.array(query_vec), top_k)
return "\n".join([texts[i] for i in I[0]])
# βœ… Gradio chatbot function (takes message and history)
def chat_fn(message, history):
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
context = get_context(message)
messages = [
{"role": "system", "content": "You are a helpful assistant. Use the following context: " + context},
{"role": "user", "content": message}
]
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
# βœ… ChatInterface now exposed at /chat
demo = gr.ChatInterface(
fn=chat_fn,
title="CODEX MIRXA KAMRAN",
description="Chat with our RAG-enabled coding assistant powered by OpenRouter",
examples=["How do I use pandas to filter rows?", "Fix this bug in my code", "What is Python typing?"],
theme="soft",
chatbot=gr.Chatbot(height=400),
api_name="//chat" # βœ… enable POST access via /chat
)
if __name__ == "__main__":
demo.launch()