Spaces:
Sleeping
Sleeping
Arbazkhan-cs
commited on
Commit
•
08bf29a
1
Parent(s):
61f1bfd
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,67 @@
|
|
1 |
-
import os
|
2 |
-
from flask import Flask, request, render_template, redirect, url_for
|
3 |
-
from werkzeug.utils import secure_filename
|
4 |
-
from utils import create_retriever_tool_agent, create_arxiv_tool_agent, google_search, get_prompt
|
5 |
-
from langchain_groq import ChatGroq
|
6 |
-
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
|
9 |
-
load_dotenv()
|
10 |
-
|
11 |
-
app = Flask(__name__, template_folder="
|
12 |
-
app.config['UPLOAD_FOLDER'] = "
|
13 |
-
app.config['ALLOWED_EXTENSIONS'] = {'pdf'}
|
14 |
-
|
15 |
-
# Initialize tools and agent
|
16 |
-
app.config["pdf_retriever_tool"] = create_retriever_tool_agent("
|
17 |
-
app.config["arxiv_tool"] = create_arxiv_tool_agent()
|
18 |
-
app.config["tools"] = [app.config["pdf_retriever_tool"], app.config["arxiv_tool"], google_search]
|
19 |
-
app.config["prompt"] = get_prompt()
|
20 |
-
app.config["agent"] = create_tool_calling_agent(
|
21 |
-
llm=ChatGroq(model="llama3-8b-8192", temperature=0.5),
|
22 |
-
tools=app.config["tools"],
|
23 |
-
prompt=app.config["prompt"]
|
24 |
-
)
|
25 |
-
app.config["agent_executor"] = AgentExecutor(agent=app.config["agent"], tools=app.config["tools"], verbose=True)
|
26 |
-
|
27 |
-
def allowed_file(filename):
|
28 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
29 |
-
|
30 |
-
@app.route('/', methods=['GET', 'POST'])
|
31 |
-
def index():
|
32 |
-
print("Index function called...")
|
33 |
-
if request.method == 'POST':
|
34 |
-
file = request.files.get('file')
|
35 |
-
if file.filename != "" and allowed_file(file.filename):
|
36 |
-
filename = secure_filename(file.filename)
|
37 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
38 |
-
file.save(file_path)
|
39 |
-
|
40 |
-
|
41 |
-
# Load the PDF using PyPDFLoader
|
42 |
-
app.config["pdf_retriever_tool"] = create_retriever_tool_agent(app.config['UPLOAD_FOLDER'])
|
43 |
-
app.config["tools"] = [app.config["pdf_retriever_tool"], app.config["arxiv_tool"], google_search]
|
44 |
-
app.config["agent"] = create_tool_calling_agent(
|
45 |
-
llm=ChatGroq(model="llama3-8b-8192", temperature=0.5),
|
46 |
-
tools=app.config["tools"],
|
47 |
-
prompt=app.config["prompt"]
|
48 |
-
)
|
49 |
-
app.config["agent_executor"] = AgentExecutor(agent=app.config["agent"], tools=app.config["tools"], verbose=True)
|
50 |
-
|
51 |
-
|
52 |
-
if 'query' in request.form:
|
53 |
-
query = request.form['query']
|
54 |
-
result = app.config["agent_executor"].invoke({"input": query})["output"]
|
55 |
-
max_line_length = 100
|
56 |
-
text = result
|
57 |
-
lines = [text[i:i+max_line_length] for i in range(0, len(text), max_line_length)]
|
58 |
-
formatted_text = "\n".join(lines)
|
59 |
-
return render_template('index.html', result=formatted_text)
|
60 |
-
|
61 |
-
return render_template('index.html')
|
62 |
-
|
63 |
-
if __name__ == '__main__':
|
64 |
-
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
65 |
-
os.makedirs(app.config['UPLOAD_FOLDER'])
|
66 |
-
print("Flask app running...")
|
67 |
-
app.run(debug=False)
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, render_template, redirect, url_for
|
3 |
+
from werkzeug.utils import secure_filename
|
4 |
+
from utils import create_retriever_tool_agent, create_arxiv_tool_agent, google_search, get_prompt
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
app = Flask(__name__, template_folder="./templates")
|
12 |
+
app.config['UPLOAD_FOLDER'] = "./uploads"
|
13 |
+
app.config['ALLOWED_EXTENSIONS'] = {'pdf'}
|
14 |
+
|
15 |
+
# Initialize tools and agent
|
16 |
+
app.config["pdf_retriever_tool"] = create_retriever_tool_agent("./Pdfs")
|
17 |
+
app.config["arxiv_tool"] = create_arxiv_tool_agent()
|
18 |
+
app.config["tools"] = [app.config["pdf_retriever_tool"], app.config["arxiv_tool"], google_search]
|
19 |
+
app.config["prompt"] = get_prompt()
|
20 |
+
app.config["agent"] = create_tool_calling_agent(
|
21 |
+
llm=ChatGroq(model="llama3-8b-8192", temperature=0.5),
|
22 |
+
tools=app.config["tools"],
|
23 |
+
prompt=app.config["prompt"]
|
24 |
+
)
|
25 |
+
app.config["agent_executor"] = AgentExecutor(agent=app.config["agent"], tools=app.config["tools"], verbose=True)
|
26 |
+
|
27 |
+
def allowed_file(filename):
|
28 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
29 |
+
|
30 |
+
@app.route('/', methods=['GET', 'POST'])
|
31 |
+
def index():
|
32 |
+
print("Index function called...")
|
33 |
+
if request.method == 'POST':
|
34 |
+
file = request.files.get('file')
|
35 |
+
if file.filename != "" and allowed_file(file.filename):
|
36 |
+
filename = secure_filename(file.filename)
|
37 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
38 |
+
file.save(file_path)
|
39 |
+
|
40 |
+
|
41 |
+
# Load the PDF using PyPDFLoader
|
42 |
+
app.config["pdf_retriever_tool"] = create_retriever_tool_agent(app.config['UPLOAD_FOLDER'])
|
43 |
+
app.config["tools"] = [app.config["pdf_retriever_tool"], app.config["arxiv_tool"], google_search]
|
44 |
+
app.config["agent"] = create_tool_calling_agent(
|
45 |
+
llm=ChatGroq(model="llama3-8b-8192", temperature=0.5),
|
46 |
+
tools=app.config["tools"],
|
47 |
+
prompt=app.config["prompt"]
|
48 |
+
)
|
49 |
+
app.config["agent_executor"] = AgentExecutor(agent=app.config["agent"], tools=app.config["tools"], verbose=True)
|
50 |
+
|
51 |
+
|
52 |
+
if 'query' in request.form:
|
53 |
+
query = request.form['query']
|
54 |
+
result = app.config["agent_executor"].invoke({"input": query})["output"]
|
55 |
+
max_line_length = 100
|
56 |
+
text = result
|
57 |
+
lines = [text[i:i+max_line_length] for i in range(0, len(text), max_line_length)]
|
58 |
+
formatted_text = "\n".join(lines)
|
59 |
+
return render_template('index.html', result=formatted_text)
|
60 |
+
|
61 |
+
return render_template('index.html')
|
62 |
+
|
63 |
+
if __name__ == '__main__':
|
64 |
+
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
65 |
+
os.makedirs(app.config['UPLOAD_FOLDER'])
|
66 |
+
print("Flask app running...")
|
67 |
+
app.run(debug=False)
|