Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,34 @@
|
|
|
|
|
|
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
|
8 |
-
from pydantic import BaseModel
|
9 |
from sentence_transformers import SentenceTransformer
|
|
|
10 |
|
11 |
-
# Load
|
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 |
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 |
def chat_fn(message, history):
|
27 |
headers = {
|
28 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -30,7 +36,10 @@ def chat_fn(message, history):
|
|
30 |
}
|
31 |
|
32 |
context = get_context(message)
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
for user, assistant in history:
|
36 |
messages.append({"role": "user", "content": user})
|
@@ -49,25 +58,24 @@ def chat_fn(message, history):
|
|
49 |
|
50 |
return reply
|
51 |
|
52 |
-
# β
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
fn=chat_fn,
|
55 |
-
title="
|
56 |
-
description="
|
57 |
theme="soft"
|
58 |
)
|
59 |
|
60 |
-
# β
FastAPI
|
61 |
-
app
|
62 |
-
|
63 |
-
|
64 |
-
app = gr.mount_gradio_app(app, gradio_ui, path="/")
|
65 |
-
|
66 |
-
# β
FastAPI API Endpoint
|
67 |
-
class ChatRequest(BaseModel):
|
68 |
-
message: str
|
69 |
-
history: list = []
|
70 |
-
|
71 |
-
@app.post("/chat")
|
72 |
-
def api_chat(req: ChatRequest):
|
73 |
-
return {"response": chat_fn(req.message, req.history)}
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
import gradio as gr
|
|
|
4 |
import os
|
5 |
+
import json
|
6 |
import faiss
|
7 |
import numpy as np
|
|
|
|
|
|
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
+
import requests
|
10 |
|
11 |
+
# β
Load RAG-related files
|
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 |
+
# β
Use your OpenRouter API key from environment
|
19 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
20 |
MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
|
21 |
|
22 |
+
# β
FastAPI app
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
# β
Context Retriever
|
26 |
def get_context(query, top_k=5):
|
27 |
query_vec = embed_model.encode([query])
|
28 |
D, I = index.search(np.array(query_vec), top_k)
|
29 |
return "\n".join([texts[i] for i in I[0]])
|
30 |
|
31 |
+
# β
Chat Function
|
32 |
def chat_fn(message, history):
|
33 |
headers = {
|
34 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
36 |
}
|
37 |
|
38 |
context = get_context(message)
|
39 |
+
|
40 |
+
messages = [
|
41 |
+
{"role": "system", "content": "You are a helpful assistant. Use the following context to answer: " + context}
|
42 |
+
]
|
43 |
|
44 |
for user, assistant in history:
|
45 |
messages.append({"role": "user", "content": user})
|
|
|
58 |
|
59 |
return reply
|
60 |
|
61 |
+
# β
FastAPI POST endpoint
|
62 |
+
@app.post("/chat")
|
63 |
+
async def chat_api(request: Request):
|
64 |
+
body = await request.json()
|
65 |
+
message = body.get("message")
|
66 |
+
history = body.get("history", [])
|
67 |
+
response = chat_fn(message, history)
|
68 |
+
return JSONResponse(content={"response": response})
|
69 |
+
|
70 |
+
# β
Gradio Chat Interface
|
71 |
+
demo = gr.ChatInterface(
|
72 |
fn=chat_fn,
|
73 |
+
title="CODEX MIRXA KAMRAN",
|
74 |
+
description="Chat with AI MODEL trained by Mirxa Kamran",
|
75 |
theme="soft"
|
76 |
)
|
77 |
|
78 |
+
# β
Mount Gradio in FastAPI
|
79 |
+
@app.get("/")
|
80 |
+
def gradio_app():
|
81 |
+
return gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|