Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,69 @@
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
from langchain_classic.agents import AgentExecutor, create_react_agent
|
| 7 |
-
from langchain_classic.tools import Tool
|
| 8 |
-
from langchain_classic.memory import ConversationBufferMemory
|
| 9 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 10 |
-
from
|
| 11 |
-
from
|
|
|
|
| 12 |
|
| 13 |
-
load_dotenv()
|
| 14 |
app = Flask(__name__)
|
| 15 |
CORS(app)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
llm = HuggingFaceEndpoint(
|
| 20 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
with DDGS() as ddgs:
|
| 29 |
-
results = list(ddgs.text(query, max_results=3))
|
| 30 |
-
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 31 |
-
except:
|
| 32 |
-
return "Search error. Try again."
|
| 33 |
-
|
| 34 |
tools = [
|
| 35 |
Tool(
|
| 36 |
-
name="web_search",
|
| 37 |
-
func=
|
| 38 |
-
description="Useful for
|
| 39 |
)
|
| 40 |
]
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
template = """Answer the following questions as best you can. You have access to the following tools:
|
| 44 |
-
|
| 45 |
-
{tools}
|
| 46 |
-
|
| 47 |
-
Use the following format:
|
| 48 |
-
|
| 49 |
-
Question: the input question you must answer
|
| 50 |
-
Thought: you should always think about what to do
|
| 51 |
-
Action: the action to take, should be one of [{tool_names}]
|
| 52 |
-
Action Input: the input to the action
|
| 53 |
-
Observation: the result of the action
|
| 54 |
-
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 55 |
-
Thought: I now know the final answer
|
| 56 |
-
Final Answer: the final answer to the original input question
|
| 57 |
-
|
| 58 |
-
Begin!
|
| 59 |
-
|
| 60 |
-
Question: {input}
|
| 61 |
-
Thought:{agent_scratchpad}"""
|
| 62 |
-
|
| 63 |
-
prompt = PromptTemplate.from_template(template)
|
| 64 |
-
|
| 65 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
handle_parsing_errors=True
|
| 73 |
)
|
| 74 |
|
| 75 |
-
@app.route(
|
| 76 |
def index():
|
| 77 |
-
return render_template(
|
| 78 |
|
| 79 |
-
@app.route(
|
| 80 |
def ask():
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
from flask_cors import CORS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
+
from langchain.agents import initialize_agent, Tool, AgentType
|
| 6 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
|
|
|
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
+
# 1. Initialize the LLM with the correct 2026 Router settings
|
| 13 |
+
# Make sure your HF Token is in your Space's 'Secrets' tab!
|
| 14 |
+
sec_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 15 |
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 18 |
+
task="text-generation",
|
| 19 |
+
huggingfacehub_api_token=sec_token,
|
| 20 |
+
timeout=300,
|
| 21 |
+
# This prevents the 404 by using the standard inference provider
|
| 22 |
+
server_kwargs={"wait_for_model": True}
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# 2. Setup Tools
|
| 26 |
+
search = DuckDuckGoSearchRun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
tools = [
|
| 28 |
Tool(
|
| 29 |
+
name="web_search",
|
| 30 |
+
func=search.run,
|
| 31 |
+
description="Useful for answering questions about current events or real-time data."
|
| 32 |
)
|
| 33 |
]
|
| 34 |
|
| 35 |
+
# 3. Setup Memory & Agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 37 |
|
| 38 |
+
agent_executor = initialize_agent(
|
| 39 |
+
tools,
|
| 40 |
+
llm,
|
| 41 |
+
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
| 42 |
+
verbose=True,
|
| 43 |
+
memory=memory,
|
| 44 |
handle_parsing_errors=True
|
| 45 |
)
|
| 46 |
|
| 47 |
+
@app.route('/')
|
| 48 |
def index():
|
| 49 |
+
return render_template('index.html')
|
| 50 |
|
| 51 |
+
@app.route('/ask', methods=['POST'])
|
| 52 |
def ask():
|
| 53 |
+
try:
|
| 54 |
+
data = request.get_json()
|
| 55 |
+
user_query = data.get("query")
|
| 56 |
+
|
| 57 |
+
# Using .invoke instead of .run for better stability in 2026
|
| 58 |
+
response = agent_executor.invoke({"input": user_query})
|
| 59 |
+
|
| 60 |
+
return jsonify({"answer": response["output"]})
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error: {str(e)}")
|
| 63 |
+
# Check if the error is a 404 to provide better feedback
|
| 64 |
+
if "404" in str(e):
|
| 65 |
+
return jsonify({"answer": "System Error: Model route not found. Please ensure you have accepted the Llama 3.2 license on Hugging Face."}), 500
|
| 66 |
+
return jsonify({"answer": f"Backend Error: {str(e)}"}), 500
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
app.run(host="0.0.0.0", port=7860)
|