Adieee5 commited on
Commit
c4ebe2e
·
verified ·
1 Parent(s): f218a6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -26
app.py CHANGED
@@ -2,25 +2,20 @@ from flask import Flask, request, jsonify, render_template
2
  from flask_cors import CORS
3
  from dotenv import load_dotenv
4
  import os
5
- import re
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain_community.vectorstores import Chroma
8
  from langchain_google_genai import ChatGoogleGenerativeAI
9
  from langchain_core.prompts import PromptTemplate
10
  from langchain.chains import RetrievalQA
11
-
12
- app = Flask(__name__)
13
  CORS(app)
14
-
15
  # Load environment variables
16
  load_dotenv()
17
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
18
  if not GOOGLE_API_KEY:
19
  raise ValueError("GOOGLE_API_KEY not found in environment variables.")
20
-
21
  # Lazy globals
22
  qa_chain = None
23
-
24
  def get_qa_chain():
25
  global qa_chain
26
  if qa_chain is None:
@@ -30,7 +25,6 @@ def get_qa_chain():
30
  google_api_key=GOOGLE_API_KEY,
31
  convert_system_message_to_human=True
32
  )
33
-
34
  # Embeddings and vector store
35
  embedding_model = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")
36
  vectordb = Chroma(
@@ -39,7 +33,6 @@ def get_qa_chain():
39
  collection_name="pdf_search_chroma"
40
  )
41
  retriever = vectordb.as_retriever(search_kwargs={"k": 6})
42
-
43
  # Prompt
44
  prompt_template = PromptTemplate.from_template("""
45
  You are an intelligent assistant for students asking about their university.
@@ -52,7 +45,6 @@ def get_qa_chain():
52
  {question}
53
  Answer:
54
  """)
55
-
56
  # Create chain
57
  qa_chain = RetrievalQA.from_chain_type(
58
  llm=llm,
@@ -61,35 +53,20 @@ def get_qa_chain():
61
  chain_type_kwargs={"prompt": prompt_template}
62
  )
63
  return qa_chain
64
-
65
- def convert_links_to_html(text):
66
- """
67
- Convert links in the format [Text][URL] to HTML <a> tags.
68
- Example: [Profile][https://example.com] becomes <a href="https://example.com">Profile</a>
69
- """
70
- pattern = r'\[(.*?)\]\[(.*?)\]'
71
- return re.sub(pattern, r'<a href="\2">\1</a>', text)
72
-
73
  @app.route("/")
74
  def home():
75
  return render_template("index.html")
76
-
77
  @app.route("/get", methods=["POST"])
78
  def get_response():
79
  data = request.get_json()
80
  query = data.get("message", "")
81
-
82
  if not query:
83
  return jsonify({"response": {"response": "No message received."}}), 400
84
-
85
  chain = get_qa_chain()
86
  try:
87
  response = chain.run(query)
88
- # Convert links to HTML format
89
- response_with_html_links = convert_links_to_html(response)
90
- return jsonify({"response": {"response": response_with_html_links}})
91
  except Exception as e:
92
  return jsonify({"response": {"response": f"Error: {str(e)}"}}), 500
93
-
94
- if __name__ == "__main__":
95
  app.run(debug=True)
 
2
  from flask_cors import CORS
3
  from dotenv import load_dotenv
4
  import os
 
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import Chroma
7
  from langchain_google_genai import ChatGoogleGenerativeAI
8
  from langchain_core.prompts import PromptTemplate
9
  from langchain.chains import RetrievalQA
10
+ app = Flask(name)
 
11
  CORS(app)
 
12
  # Load environment variables
13
  load_dotenv()
14
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
15
  if not GOOGLE_API_KEY:
16
  raise ValueError("GOOGLE_API_KEY not found in environment variables.")
 
17
  # Lazy globals
18
  qa_chain = None
 
19
  def get_qa_chain():
20
  global qa_chain
21
  if qa_chain is None:
 
25
  google_api_key=GOOGLE_API_KEY,
26
  convert_system_message_to_human=True
27
  )
 
28
  # Embeddings and vector store
29
  embedding_model = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")
30
  vectordb = Chroma(
 
33
  collection_name="pdf_search_chroma"
34
  )
35
  retriever = vectordb.as_retriever(search_kwargs={"k": 6})
 
36
  # Prompt
37
  prompt_template = PromptTemplate.from_template("""
38
  You are an intelligent assistant for students asking about their university.
 
45
  {question}
46
  Answer:
47
  """)
 
48
  # Create chain
49
  qa_chain = RetrievalQA.from_chain_type(
50
  llm=llm,
 
53
  chain_type_kwargs={"prompt": prompt_template}
54
  )
55
  return qa_chain
 
 
 
 
 
 
 
 
 
56
  @app.route("/")
57
  def home():
58
  return render_template("index.html")
 
59
  @app.route("/get", methods=["POST"])
60
  def get_response():
61
  data = request.get_json()
62
  query = data.get("message", "")
 
63
  if not query:
64
  return jsonify({"response": {"response": "No message received."}}), 400
 
65
  chain = get_qa_chain()
66
  try:
67
  response = chain.run(query)
68
+ return jsonify({"response": {"response": response}})
 
 
69
  except Exception as e:
70
  return jsonify({"response": {"response": f"Error: {str(e)}"}}), 500
71
+ if name == "main":
 
72
  app.run(debug=True)