rahul-02 commited on
Commit
9768dbd
·
verified ·
1 Parent(s): ef77bd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -52
app.py CHANGED
@@ -1,66 +1,44 @@
1
  import os
2
  from flask import Flask, render_template, request, jsonify
3
- from flask_cors import CORS
4
  from huggingface_hub import InferenceClient
5
- from duckduckgo_search import DDGS # Direct import for stability
6
 
7
  app = Flask(__name__)
8
- CORS(app)
9
 
10
- client = InferenceClient(
11
- model="meta-llama/Llama-3.2-3B-Instruct",
12
- token=os.getenv("HF_TOKEN")
13
- )
14
 
15
- def get_live_data(query):
16
- """Fetches real-time snippets from the web."""
17
- try:
18
- with DDGS() as ddgs:
19
- results = [r['body'] for r in ddgs.text(query, max_results=3)]
20
- return "\n".join(results)
21
- except Exception:
22
- return "No real-time data available at the moment."
23
 
24
- @app.route('/')
25
- def index():
26
- return render_template('index.html')
 
 
 
 
 
 
 
27
 
28
  @app.route('/ask', methods=['POST'])
29
  def ask():
30
- try:
31
- data = request.get_json()
32
- user_query = data.get("query")
33
-
34
- # 1. Get real-time info
35
- web_context = get_live_data(user_query)
36
-
37
- # 2. Build the 'Augmented' Prompt
38
- system_prompt = f"""You are a helpful research assistant.
39
- Use the following real-time web context to answer the user's request.
40
- If the context is empty, rely on your internal knowledge.
41
-
42
- WEB CONTEXT:
43
- {web_context}
44
- """
45
-
46
- # 3. Call Llama
47
- response = ""
48
- for message in client.chat_completion(
49
- messages=[
50
- {"role": "system", "content": system_prompt},
51
- {"role": "user", "content": user_query}
52
- ],
53
- max_tokens=800,
54
- stream=True,
55
- ):
56
- token = message.choices[0].delta.content
57
- if token:
58
- response += token
59
-
60
- return jsonify({"answer": response})
61
-
62
- except Exception as e:
63
- return jsonify({"answer": f"System Error: {str(e)}"}), 500
64
 
65
  if __name__ == "__main__":
66
  app.run(host="0.0.0.0", port=7860)
 
1
  import os
2
  from flask import Flask, render_template, request, jsonify
3
+ import chromadb
4
  from huggingface_hub import InferenceClient
5
+ from sentence_transformers import SentenceTransformer
6
 
7
  app = Flask(__name__)
 
8
 
9
+ client = InferenceClient(model="meta-llama/Llama-3.2-3B-Instruct", token=os.getenv("HF_TOKEN"))
 
 
 
10
 
11
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
12
+ db_client = chromadb.PersistentClient(path="./my_vector_db")
13
+ collection = db_client.get_or_create_collection(name="research_docs")
 
 
 
 
 
14
 
15
+ def add_to_knowledge_base(text_list):
16
+ embeddings = embed_model.encode(text_list).tolist()
17
+ ids = [f"id_{i}" for i in range(len(text_list))]
18
+ collection.add(documents=text_list, embeddings=embeddings, ids=ids)
19
+
20
+ initial_data = [
21
+ "The 2026 World AI Summit took place in Tokyo, focusing on decentralized models.",
22
+ "Nexus AI Research OS was launched in January 2026 as a browser-based tool.",
23
+ ]
24
+ add_to_knowledge_base(initial_data)
25
 
26
  @app.route('/ask', methods=['POST'])
27
  def ask():
28
+ user_query = request.get_json().get("query")
29
+
30
+ query_vec = embed_model.encode([user_query]).tolist()
31
+ results = collection.query(query_embeddings=query_vec, n_results=2)
32
+ retrieved_text = "\n".join(results['documents'][0])
33
+
34
+ prompt = f"Use this data to answer: {retrieved_text}\n\nQuestion: {user_query}"
35
+
36
+ response = client.chat_completion(
37
+ messages=[{"role": "user", "content": prompt}],
38
+ max_tokens=500
39
+ )
40
+
41
+ return jsonify({"answer": response.choices[0].message.content})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
  app.run(host="0.0.0.0", port=7860)