Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import faiss
|
5 |
+
import numpy as np
|
6 |
+
import json
|
7 |
+
from fastapi import FastAPI, Request
|
8 |
+
from pydantic import BaseModel
|
9 |
+
from sentence_transformers import SentenceTransformer
|
10 |
+
|
11 |
+
# β
Load vector data
|
12 |
+
with open("texts.json", "r", encoding="utf-8") as f:
|
13 |
+
texts = json.load(f)
|
14 |
+
|
15 |
+
index = faiss.read_index("faiss_index.bin")
|
16 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
17 |
+
|
18 |
+
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
19 |
+
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
20 |
+
|
21 |
+
# β
Semantic search
|
22 |
+
def get_context(query, top_k=5):
|
23 |
+
query_vec = embed_model.encode([query])
|
24 |
+
D, I = index.search(np.array(query_vec), top_k)
|
25 |
+
return "\n".join([texts[i] for i in I[0]])
|
26 |
+
|
27 |
+
# β
Chatbot response
|
28 |
+
def chat_fn(message, history):
|
29 |
+
headers = {
|
30 |
+
"Authorization": f"Bearer {API_KEY}",
|
31 |
+
"Content-Type": "application/json"
|
32 |
+
}
|
33 |
+
|
34 |
+
context = get_context(message)
|
35 |
+
messages = [{"role": "system", "content": f"You are CODEX Assistant by Mirxa Kamran. Use this context:\n{context}"}]
|
36 |
+
|
37 |
+
for user, assistant in history:
|
38 |
+
messages.append({"role": "user", "content": user})
|
39 |
+
messages.append({"role": "assistant", "content": assistant})
|
40 |
+
|
41 |
+
messages.append({"role": "user", "content": message})
|
42 |
+
|
43 |
+
payload = {"model": MODEL, "messages": messages}
|
44 |
+
|
45 |
+
try:
|
46 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
47 |
+
response.raise_for_status()
|
48 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
49 |
+
except Exception as e:
|
50 |
+
reply = f"β Error: {e}"
|
51 |
+
|
52 |
+
return reply
|
53 |
+
|
54 |
+
# β
Gradio UI
|
55 |
+
demo = gr.ChatInterface(
|
56 |
+
fn=chat_fn,
|
57 |
+
title="π» CODEX Assistant by Mirxa Kamran",
|
58 |
+
description="Chat with a context-aware AI code assistant.",
|
59 |
+
theme="soft"
|
60 |
+
)
|
61 |
+
|
62 |
+
# β
FastAPI app
|
63 |
+
app = FastAPI()
|
64 |
+
|
65 |
+
# β
Mount Gradio on root path
|
66 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
67 |
+
|
68 |
+
# β
FastAPI POST API endpoint
|
69 |
+
class ChatRequest(BaseModel):
|
70 |
+
message: str
|
71 |
+
history: list = []
|
72 |
+
|
73 |
+
@app.post("/chat")
|
74 |
+
def api_chat(req: ChatRequest):
|
75 |
+
reply = chat_fn(req.message, req.history)
|
76 |
+
return {"response": reply}
|
77 |
+
|
78 |
+
# β
Run manually in local dev or on Spaces
|
79 |
+
if __name__ == "__main__":
|
80 |
+
import uvicorn
|
81 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|