Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,15 @@
|
|
1 |
import os
|
2 |
import re
|
3 |
import logging
|
4 |
-
from fastapi import FastAPI, HTTPException
|
5 |
-
#from fastapi.middleware.cors import CORSMiddleware
|
6 |
-
from fastapi.responses import StreamingResponse
|
7 |
from fastapi.responses import RedirectResponse
|
8 |
-
import subprocess
|
9 |
from pydantic import BaseModel
|
10 |
from langchain.chains import RetrievalQA
|
11 |
from langchain.prompts import PromptTemplate
|
12 |
from langchain_community.llms import CTransformers
|
13 |
from langchain_community.vectorstores import FAISS
|
14 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
15 |
-
import
|
16 |
-
import uvicorn
|
17 |
-
from threading import Thread
|
18 |
-
import requests
|
19 |
from dotenv import load_dotenv
|
20 |
|
21 |
# Load environment variables
|
@@ -25,11 +19,9 @@ load_dotenv()
|
|
25 |
logging.basicConfig(level=logging.INFO)
|
26 |
logger = logging.getLogger(__name__)
|
27 |
|
28 |
-
|
29 |
# FastAPI app
|
30 |
app = FastAPI()
|
31 |
|
32 |
-
|
33 |
# Load embeddings and vector database
|
34 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
|
35 |
try:
|
@@ -82,25 +74,18 @@ class AnswerResponse(BaseModel):
|
|
82 |
def clean_answer(answer):
|
83 |
# Remove unnecessary characters and symbols
|
84 |
cleaned_answer = re.sub(r'[^\w\s.,-]', '', answer)
|
85 |
-
|
86 |
# Remove repetitive phrases by identifying repeated words or sequences
|
87 |
cleaned_answer = re.sub(r'\b(\w+)( \1\b)+', r'\1', cleaned_answer)
|
88 |
-
|
89 |
# Remove any trailing or leading spaces
|
90 |
cleaned_answer = cleaned_answer.strip()
|
91 |
-
|
92 |
# Replace multiple spaces with a single space
|
93 |
cleaned_answer = re.sub(r'\s+', ' ', cleaned_answer)
|
94 |
-
|
95 |
# Replace \n with newline character in markdown
|
96 |
cleaned_answer = re.sub(r'\\n', '\n', cleaned_answer)
|
97 |
-
|
98 |
# Check for bullet points and replace with markdown syntax
|
99 |
cleaned_answer = re.sub(r'^\s*-\s+(.*)$', r'* \1', cleaned_answer, flags=re.MULTILINE)
|
100 |
-
|
101 |
# Check for numbered lists and replace with markdown syntax
|
102 |
cleaned_answer = re.sub(r'^\s*\d+\.\s+(.*)$', r'1. \1', cleaned_answer, flags=re.MULTILINE)
|
103 |
-
|
104 |
# Check for headings and replace with markdown syntax
|
105 |
cleaned_answer = re.sub(r'^\s*(#+)\s+(.*)$', r'\1 \2', cleaned_answer, flags=re.MULTILINE)
|
106 |
|
@@ -135,14 +120,12 @@ async def query(question_request: QuestionRequest):
|
|
135 |
# Clean up the answer
|
136 |
cleaned_answer = clean_answer(answer)
|
137 |
|
138 |
-
# Return cleaned_answer wrapped in a dictionary
|
139 |
return {"answer": cleaned_answer}
|
140 |
|
141 |
except Exception as e:
|
142 |
logger.error(f"Error processing query: {e}")
|
143 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
144 |
|
145 |
-
|
146 |
def run_streamlit():
|
147 |
subprocess.Popen(["streamlit", "run", "frontend.py", "--server.port", "8501"])
|
148 |
|
@@ -154,6 +137,5 @@ async def startup_event():
|
|
154 |
async def root():
|
155 |
return RedirectResponse(url="http://localhost:8501")
|
156 |
|
157 |
-
|
158 |
#if __name__ == '__main__':
|
159 |
#uvicorn.run(app, host='0.0.0.0', port=7860)
|
|
|
1 |
import os
|
2 |
import re
|
3 |
import logging
|
4 |
+
from fastapi import FastAPI, HTTPException
|
|
|
|
|
5 |
from fastapi.responses import RedirectResponse
|
|
|
6 |
from pydantic import BaseModel
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from langchain.prompts import PromptTemplate
|
9 |
from langchain_community.llms import CTransformers
|
10 |
from langchain_community.vectorstores import FAISS
|
11 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
12 |
+
import subprocess
|
|
|
|
|
|
|
13 |
from dotenv import load_dotenv
|
14 |
|
15 |
# Load environment variables
|
|
|
19 |
logging.basicConfig(level=logging.INFO)
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
|
|
22 |
# FastAPI app
|
23 |
app = FastAPI()
|
24 |
|
|
|
25 |
# Load embeddings and vector database
|
26 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
|
27 |
try:
|
|
|
74 |
def clean_answer(answer):
|
75 |
# Remove unnecessary characters and symbols
|
76 |
cleaned_answer = re.sub(r'[^\w\s.,-]', '', answer)
|
|
|
77 |
# Remove repetitive phrases by identifying repeated words or sequences
|
78 |
cleaned_answer = re.sub(r'\b(\w+)( \1\b)+', r'\1', cleaned_answer)
|
|
|
79 |
# Remove any trailing or leading spaces
|
80 |
cleaned_answer = cleaned_answer.strip()
|
|
|
81 |
# Replace multiple spaces with a single space
|
82 |
cleaned_answer = re.sub(r'\s+', ' ', cleaned_answer)
|
|
|
83 |
# Replace \n with newline character in markdown
|
84 |
cleaned_answer = re.sub(r'\\n', '\n', cleaned_answer)
|
|
|
85 |
# Check for bullet points and replace with markdown syntax
|
86 |
cleaned_answer = re.sub(r'^\s*-\s+(.*)$', r'* \1', cleaned_answer, flags=re.MULTILINE)
|
|
|
87 |
# Check for numbered lists and replace with markdown syntax
|
88 |
cleaned_answer = re.sub(r'^\s*\d+\.\s+(.*)$', r'1. \1', cleaned_answer, flags=re.MULTILINE)
|
|
|
89 |
# Check for headings and replace with markdown syntax
|
90 |
cleaned_answer = re.sub(r'^\s*(#+)\s+(.*)$', r'\1 \2', cleaned_answer, flags=re.MULTILINE)
|
91 |
|
|
|
120 |
# Clean up the answer
|
121 |
cleaned_answer = clean_answer(answer)
|
122 |
|
|
|
123 |
return {"answer": cleaned_answer}
|
124 |
|
125 |
except Exception as e:
|
126 |
logger.error(f"Error processing query: {e}")
|
127 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
128 |
|
|
|
129 |
def run_streamlit():
|
130 |
subprocess.Popen(["streamlit", "run", "frontend.py", "--server.port", "8501"])
|
131 |
|
|
|
137 |
async def root():
|
138 |
return RedirectResponse(url="http://localhost:8501")
|
139 |
|
|
|
140 |
#if __name__ == '__main__':
|
141 |
#uvicorn.run(app, host='0.0.0.0', port=7860)
|