Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,47 @@
|
|
| 1 |
import os
|
| 2 |
-
from flask import Flask,
|
| 3 |
-
import chromadb
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
-
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
]
|
| 24 |
-
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
retrieved_text = "\n".join(results['documents'][0])
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|