mirxakamran893 commited on
Commit
5836b9c
Β·
verified Β·
1 Parent(s): 55e3b14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
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 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
  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
- messages = [{"role": "system", "content": f"You are CODEX Assistant. Use this:\n{context}"}]
 
 
 
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
- # βœ… Gradio Interface
53
- gradio_ui = gr.ChatInterface(
 
 
 
 
 
 
 
 
 
54
  fn=chat_fn,
55
- title="πŸ’» CODEX Assistant",
56
- description="Ask me coding or technical questions!",
57
  theme="soft"
58
  )
59
 
60
- # βœ… FastAPI App
61
- app = FastAPI()
62
-
63
- # βœ… Mount Gradio at `/`
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="/")