mirxakamran893 commited on
Commit
0527279
Β·
verified Β·
1 Parent(s): e5f3c38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -1,4 +1,5 @@
1
- import gradio as gr
 
2
  import requests
3
  import os
4
  import faiss
@@ -6,35 +7,53 @@ import numpy as np
6
  import json
7
  from sentence_transformers import SentenceTransformer
8
 
9
- # βœ… Load context data
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
- API_KEY = os.environ.get("OPENROUTER_API_KEY")
 
17
  MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
18
 
19
- # βœ… Get top-k context from FAISS
 
 
 
 
 
 
 
 
20
  def get_context(query, top_k=5):
21
  query_vec = embed_model.encode([query])
22
  D, I = index.search(np.array(query_vec), top_k)
23
  return "\n".join([texts[i] for i in I[0]])
24
 
25
- # βœ… Gradio chatbot function (takes message and history)
26
- def chat_fn(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  headers = {
28
  "Authorization": f"Bearer {API_KEY}",
29
  "Content-Type": "application/json"
30
  }
31
 
32
- context = get_context(message)
33
- messages = [
34
- {"role": "system", "content": "You are a helpful assistant. Use the following context: " + context},
35
- {"role": "user", "content": message}
36
- ]
37
-
38
  payload = {
39
  "model": MODEL,
40
  "messages": messages
@@ -43,22 +62,9 @@ def chat_fn(message, history):
43
  try:
44
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
45
  response.raise_for_status()
46
- reply = response.json()["choices"][0]["message"]["content"]
 
47
  except Exception as e:
48
- reply = f"❌ Error: {e}"
49
-
50
- return reply
51
-
52
- # βœ… ChatInterface now exposed at /chat
53
- demo = gr.ChatInterface(
54
- fn=chat_fn,
55
- title="CODEX MIRXA KAMRAN",
56
- description="Chat with our RAG-enabled coding assistant powered by OpenRouter",
57
- examples=["How do I use pandas to filter rows?", "Fix this bug in my code", "What is Python typing?"],
58
- theme="soft",
59
- chatbot=gr.Chatbot(height=400),
60
- api_name="/chat"
61
- )
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  import requests
4
  import os
5
  import faiss
 
7
  import json
8
  from sentence_transformers import SentenceTransformer
9
 
10
+ # βœ… Load your texts and vector index
11
  with open("texts.json", "r", encoding="utf-8") as f:
12
  texts = json.load(f)
13
 
14
  index = faiss.read_index("faiss_index.bin")
15
  embed_model = SentenceTransformer("all-MiniLM-L6-v2")
16
 
17
+ # βœ… Setup your API key and model name
18
+ API_KEY = os.environ.get("OPENROUTER_API_KEY") # Set this in environment or .env file
19
  MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
20
 
21
+ # βœ… Define FastAPI app
22
+ app = FastAPI()
23
+
24
+ # βœ… Request schema
25
+ class ChatRequest(BaseModel):
26
+ message: str
27
+ history: list = [] # Optional, not used in current logic
28
+
29
+ # βœ… Function to get top-k similar chunks from FAISS
30
  def get_context(query, top_k=5):
31
  query_vec = embed_model.encode([query])
32
  D, I = index.search(np.array(query_vec), top_k)
33
  return "\n".join([texts[i] for i in I[0]])
34
 
35
+ # βœ… Endpoint
36
+ @app.post("/chat")
37
+ def chat_api(data: ChatRequest):
38
+ user_message = data.message
39
+ context = get_context(user_message)
40
+
41
+ messages = [
42
+ {
43
+ "role": "system",
44
+ "content": f"You are a helpful coding assistant. Use the following context to answer the question:\n{context}"
45
+ },
46
+ {
47
+ "role": "user",
48
+ "content": user_message
49
+ }
50
+ ]
51
+
52
  headers = {
53
  "Authorization": f"Bearer {API_KEY}",
54
  "Content-Type": "application/json"
55
  }
56
 
 
 
 
 
 
 
57
  payload = {
58
  "model": MODEL,
59
  "messages": messages
 
62
  try:
63
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
64
  response.raise_for_status()
65
+ result = response.json()
66
+ reply = result["choices"][0]["message"]["content"]
67
  except Exception as e:
68
+ reply = f"❌ Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ return {"reply": reply}