Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,25 @@ import os
|
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
from huggingface_hub import InferenceClient
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
| 8 |
|
| 9 |
-
# 1. Initialize the Official Client
|
| 10 |
-
# This uses your HF_TOKEN secret automatically
|
| 11 |
client = InferenceClient(
|
| 12 |
model="meta-llama/Llama-3.2-3B-Instruct",
|
| 13 |
token=os.getenv("HF_TOKEN")
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.route('/')
|
| 17 |
def index():
|
| 18 |
return render_template('index.html')
|
|
@@ -23,11 +31,26 @@ def ask():
|
|
| 23 |
data = request.get_json()
|
| 24 |
user_query = data.get("query")
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
response = ""
|
| 28 |
for message in client.chat_completion(
|
| 29 |
-
messages=[
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
stream=True,
|
| 32 |
):
|
| 33 |
token = message.choices[0].delta.content
|
|
@@ -37,9 +60,7 @@ def ask():
|
|
| 37 |
return jsonify({"answer": response})
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
-
# This will tell us EXACTLY if the token is the problem
|
| 42 |
-
return jsonify({"answer": f"System Status: {str(e)}"}), 500
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 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')
|
|
|
|
| 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
|
|
|
|
| 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)
|