rahul-02 commited on
Commit
4a14205
·
verified ·
1 Parent(s): b378208

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -1,44 +1,47 @@
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('/')
27
- def index():
28
- return render_template('index.html')
29
 
30
  @app.route('/ask', methods=['POST'])
31
  def ask():
32
  user_query = request.get_json().get("query")
33
 
34
- query_vec = embed_model.encode([user_query]).tolist()
35
- results = collection.query(query_embeddings=query_vec, n_results=2)
36
- retrieved_text = "\n".join(results['documents'][0])
37
 
38
- prompt = f"Use this data to answer: {retrieved_text}\n\nQuestion: {user_query}"
 
 
 
39
 
40
- response = client.chat_completion(
41
- messages=[{"role": "user", "content": prompt}],
 
 
 
 
 
42
  max_tokens=500
43
  )
44
 
 
1
  import os
2
+ from flask import Flask, request, jsonify
 
3
  from huggingface_hub import InferenceClient
4
+ import requests
5
 
6
  app = Flask(__name__)
7
 
8
+ # 1. Setup Clients
9
+ hf_client = InferenceClient(model="meta-llama/Llama-3.2-3B-Instruct", token=os.getenv("HF_TOKEN"))
10
+ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
11
+
12
+ def search_tavily(query):
13
+ """Someone else's free API doing the hard work for you."""
14
+ url = "https://api.tavily.com/search"
15
+ payload = {
16
+ "api_key": TAVILY_API_KEY,
17
+ "query": query,
18
+ "search_depth": "basic",
19
+ "max_results": 3
20
+ }
21
+ response = requests.post(url, json=payload)
22
+ results = response.json().get("results", [])
23
+ # Join snippets into one string
24
+ return "\n".join([f"Source: {r['url']}\nContent: {r['content']}" for r in results])
 
 
 
25
 
26
  @app.route('/ask', methods=['POST'])
27
  def ask():
28
  user_query = request.get_json().get("query")
29
 
30
+ # STEP 1: Get real-time data from Tavily API
31
+ web_data = search_tavily(user_query)
 
32
 
33
+ # STEP 2: Use Llama 3.2 to synthesize the answer
34
+ system_prompt = f"""You are a 2026 AI Researcher.
35
+ Use the following LIVE WEB DATA to answer.
36
+ If the data is about the PM of India or Super Bowl, use it!
37
 
38
+ DATA: {web_data}"""
39
+
40
+ response = hf_client.chat_completion(
41
+ messages=[
42
+ {"role": "system", "content": system_prompt},
43
+ {"role": "user", "content": user_query}
44
+ ],
45
  max_tokens=500
46
  )
47