File size: 1,677 Bytes
ebbf92b
aa09738
b78bb65
4a14205
ebbf92b
 
 
4a14205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b378208
1a078fb
 
 
 
6240f08
ebbf92b
9768dbd
 
4a14205
 
9768dbd
4a14205
 
 
 
9768dbd
4a14205
 
 
 
 
 
 
9768dbd
 
 
 
ebbf92b
 
9f8c4dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
from flask import Flask, request, jsonify, render_template
from huggingface_hub import InferenceClient
import requests

app = Flask(__name__)

# 1. Setup Clients
hf_client = InferenceClient(model="meta-llama/Llama-3.2-3B-Instruct", token=os.getenv("HF_TOKEN"))
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")

def search_tavily(query):
    """Someone else's free API doing the hard work for you."""
    url = "https://api.tavily.com/search"
    payload = {
        "api_key": TAVILY_API_KEY,
        "query": query,
        "search_depth": "basic",
        "max_results": 3
    }
    response = requests.post(url, json=payload)
    results = response.json().get("results", [])
    # Join snippets into one string
    return "\n".join([f"Source: {r['url']}\nContent: {r['content']}" for r in results])

@app.route('/')
def index():
    return render_template('index.html')
    
@app.route('/ask', methods=['POST'])
def ask():
    user_query = request.get_json().get("query")
    
    # STEP 1: Get real-time data from Tavily API
    web_data = search_tavily(user_query)

    # STEP 2: Use Llama 3.2 to synthesize the answer
    system_prompt = f"""You are a 2026 AI Researcher. 
    Use the following LIVE WEB DATA to answer. 
    If the data is about the PM of India or Super Bowl, use it!
    
    DATA: {web_data}"""

    response = hf_client.chat_completion(
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_query}
        ],
        max_tokens=500
    )
    
    return jsonify({"answer": response.choices[0].message.content})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)