mirxakamran893 commited on
Commit
a4b560c
Β·
verified Β·
1 Parent(s): d1dab46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -30
app.py CHANGED
@@ -6,41 +6,34 @@ import numpy as np
6
  import json
7
  from sentence_transformers import SentenceTransformer
8
 
9
- # βœ… Load 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
- # βœ… Together AI Setup
17
- API_KEY = os.environ.get("TOGETHER_API_KEY") or "76615a7c686e20c0ee8cae288fddc7ab35ae51e768abc45b3defb5b1850d3dd0"
18
- MODEL = "rekaai/reka-flash-3:free"
19
 
20
- # βœ… Context retriever
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
- context_chunks = [texts[i] for i in I[0] if i < len(texts)]
25
- return "\n".join(context_chunks).strip()
26
 
27
- # βœ… Chat function using Together API
28
  def chat_fn(message, history):
29
- context = get_context(message)
30
-
31
- if not context:
32
- return "⚠️ Sorry, no relevant data found in the provided dataset."
33
-
34
  headers = {
35
  "Authorization": f"Bearer {API_KEY}",
36
  "Content-Type": "application/json"
37
  }
38
 
 
 
39
  messages = [
40
- {
41
- "role": "system",
42
- "content": "You are a helpful assistant. Use ONLY the provided context to answer. Do not use any external data.\n\nContext:\n" + context
43
- }
44
  ]
45
 
46
  for user, assistant in history:
@@ -51,13 +44,11 @@ def chat_fn(message, history):
51
 
52
  payload = {
53
  "model": MODEL,
54
- "messages": messages,
55
- "temperature": 0.7,
56
- "max_tokens": 512
57
  }
58
 
59
  try:
60
- response = requests.post("https://api.together.xyz/v1/chat/completions", headers=headers, json=payload)
61
  response.raise_for_status()
62
  reply = response.json()["choices"][0]["message"]["content"]
63
  except Exception as e:
@@ -65,13 +56,10 @@ def chat_fn(message, history):
65
 
66
  return reply
67
 
68
- # βœ… Gradio chat interface
69
- chat_ui = gr.ChatInterface(
70
  fn=chat_fn,
71
- title="",
72
- description="",
73
- examples=[],
74
- cache_examples=False
75
- )
76
-
77
- chat_ui.launch(inbrowser=True, share=True)
 
6
  import json
7
  from sentence_transformers import SentenceTransformer
8
 
9
+ # βœ… Load RAG-related files
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
+ # βœ… Use your OpenRouter API key
17
+ API_KEY = os.environ.get("OPENROUTER_API_KEY")
18
+ MODEL = "qwen/qwen-2.5-coder-32b-instruct:free"
19
 
20
+ # βœ… Function to search relevant context
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
+ # βœ… Chat handler function
27
  def chat_fn(message, history):
 
 
 
 
 
28
  headers = {
29
  "Authorization": f"Bearer {API_KEY}",
30
  "Content-Type": "application/json"
31
  }
32
 
33
+ context = get_context(message)
34
+
35
  messages = [
36
+ {"role": "system", "content": "You are a helpful assistant. Use the following context to answer: " + context}
 
 
 
37
  ]
38
 
39
  for user, assistant in history:
 
44
 
45
  payload = {
46
  "model": MODEL,
47
+ "messages": messages
 
 
48
  }
49
 
50
  try:
51
+ response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
52
  response.raise_for_status()
53
  reply = response.json()["choices"][0]["message"]["content"]
54
  except Exception as e:
 
56
 
57
  return reply
58
 
59
+ # βœ… Launch Gradio ChatInterface
60
+ gr.ChatInterface(
61
  fn=chat_fn,
62
+ title="CODEX MIRXA KAMRAN",
63
+ description="Chat with AI MODEL trained By Mirxa Kamran",
64
+ theme="soft"
65
+ ).launch()