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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -2,7 +2,7 @@ from flask import Flask, request, jsonify, render_template
2
  from flask_cors import CORS
3
  from dotenv import load_dotenv
4
  import os
5
-
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain_community.vectorstores import Chroma
8
  from langchain_google_genai import ChatGoogleGenerativeAI
@@ -30,7 +30,7 @@ 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 +39,7 @@ 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 +52,7 @@ def get_qa_chain():
52
  {question}
53
  Answer:
54
  """)
55
-
56
  # Create chain
57
  qa_chain = RetrievalQA.from_chain_type(
58
  llm=llm,
@@ -62,6 +62,14 @@ def get_qa_chain():
62
  )
63
  return qa_chain
64
 
 
 
 
 
 
 
 
 
65
  @app.route("/")
66
  def home():
67
  return render_template("index.html")
@@ -70,15 +78,18 @@ def home():
70
  def get_response():
71
  data = request.get_json()
72
  query = data.get("message", "")
 
73
  if not query:
74
  return jsonify({"response": {"response": "No message received."}}), 400
75
-
76
  chain = get_qa_chain()
77
  try:
78
  response = chain.run(query)
79
- return jsonify({"response": {"response": response}})
 
 
80
  except Exception as e:
81
  return jsonify({"response": {"response": f"Error: {str(e)}"}}), 500
82
 
83
  if __name__ == "__main__":
84
- app.run(debug=True)
 
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
 
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
  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
  {question}
53
  Answer:
54
  """)
55
+
56
  # Create chain
57
  qa_chain = RetrievalQA.from_chain_type(
58
  llm=llm,
 
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")
 
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)