Spaces:
Running
Running
Initial commit
Browse files- app.py +107 -0
- index.html +0 -19
- requirements.txt +135 -0
- static/index.html +30 -0
- static/script.js +90 -0
- static/styles.css +0 -0
- style.css +0 -28
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from langchain.chains import LLMChain
|
| 6 |
+
from langchain.vectorstores import Chroma
|
| 7 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
+
from langchain_groq import ChatGroq
|
| 10 |
+
from langchain.memory import VectorStoreRetrieverMemory
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
import re
|
| 13 |
+
from io import BytesIO
|
| 14 |
+
from PyPDF2 import PdfReader
|
| 15 |
+
import docx
|
| 16 |
+
|
| 17 |
+
class GroqAIChatApp:
|
| 18 |
+
def __init__(self, api_key: str, persist_directory: str = "./chroma_db"):
|
| 19 |
+
self.embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
+
self.vectorstore = Chroma(persist_directory=persist_directory, embedding_function=self.embedding_model)
|
| 21 |
+
self.memory = VectorStoreRetrieverMemory(retriever=self.vectorstore.as_retriever())
|
| 22 |
+
self.llm = ChatGroq(api_key=api_key)
|
| 23 |
+
|
| 24 |
+
self.prompt_template = PromptTemplate(
|
| 25 |
+
input_variables=["history", "question"],
|
| 26 |
+
template="""You are an AI assistant. Use the context below to answer the question accurately.
|
| 27 |
+
|
| 28 |
+
Context:
|
| 29 |
+
{history}
|
| 30 |
+
|
| 31 |
+
Question: {question}
|
| 32 |
+
|
| 33 |
+
Answer:"""
|
| 34 |
+
)
|
| 35 |
+
self.conversation_chain = LLMChain(llm=self.llm, prompt=self.prompt_template, memory=self.memory)
|
| 36 |
+
|
| 37 |
+
def generate_response(self, question: str) -> str:
|
| 38 |
+
return self.conversation_chain.run({"question": question})
|
| 39 |
+
|
| 40 |
+
def process_file(self, file: UploadFile) -> str:
|
| 41 |
+
try:
|
| 42 |
+
if file.filename.endswith(".pdf"):
|
| 43 |
+
pdf_reader = PdfReader(BytesIO(file.file.read()))
|
| 44 |
+
text = "\n".join([page.extract_text() for page in pdf_reader.pages])
|
| 45 |
+
elif file.filename.endswith(".docx"):
|
| 46 |
+
doc = docx.Document(BytesIO(file.file.read()))
|
| 47 |
+
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
| 48 |
+
elif file.filename.endswith(".txt"):
|
| 49 |
+
text = file.file.read().decode("utf-8")
|
| 50 |
+
else:
|
| 51 |
+
raise ValueError("Unsupported file format")
|
| 52 |
+
|
| 53 |
+
text = re.sub(r'\s+', ' ', text)
|
| 54 |
+
return text
|
| 55 |
+
except Exception as e:
|
| 56 |
+
raise ValueError(f"Error processing file: {str(e)}")
|
| 57 |
+
|
| 58 |
+
def save_text_embeddings(self, text: str, metadata: dict):
|
| 59 |
+
try:
|
| 60 |
+
self.vectorstore.add_texts([text], metadatas=[metadata])
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise ValueError(f"Error saving embeddings: {str(e)}")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
api_key = "your_groq_api_key" # Replace with your Groq API key
|
| 66 |
+
chat_app = GroqAIChatApp(api_key=api_key)
|
| 67 |
+
|
| 68 |
+
app = FastAPI()
|
| 69 |
+
|
| 70 |
+
app.add_middleware(
|
| 71 |
+
CORSMiddleware,
|
| 72 |
+
allow_origins=["*"],
|
| 73 |
+
allow_credentials=True,
|
| 74 |
+
allow_methods=["*"],
|
| 75 |
+
allow_headers=["*"],
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
@app.get("/", response_class=HTMLResponse)
|
| 79 |
+
async def serve_frontend():
|
| 80 |
+
with open("static/index.html", "r") as file:
|
| 81 |
+
return HTMLResponse(content=file.read())
|
| 82 |
+
|
| 83 |
+
@app.post("/qa")
|
| 84 |
+
async def qa_endpoint(request: Request):
|
| 85 |
+
try:
|
| 86 |
+
data = await request.json()
|
| 87 |
+
question = data.get("question", "")
|
| 88 |
+
if not question:
|
| 89 |
+
return JSONResponse({"error": "No question provided."}, status_code=400)
|
| 90 |
+
|
| 91 |
+
answer = chat_app.generate_response(question)
|
| 92 |
+
return {"question": question, "answer": answer}
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 95 |
+
|
| 96 |
+
@app.post("/upload")
|
| 97 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 98 |
+
try:
|
| 99 |
+
text = chat_app.process_file(file)
|
| 100 |
+
metadata = {"filename": file.filename}
|
| 101 |
+
chat_app.save_text_embeddings(text, metadata)
|
| 102 |
+
return {"message": "File processed and embeddings saved successfully."}
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
| 105 |
+
|
| 106 |
+
current_dir = Path(__file__).parent
|
| 107 |
+
app.mount("/static", StaticFiles(directory=current_dir / "static"), name="static")
|
index.html
DELETED
|
@@ -1,19 +0,0 @@
|
|
| 1 |
-
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
<head>
|
| 4 |
-
<meta charset="utf-8" />
|
| 5 |
-
<meta name="viewport" content="width=device-width" />
|
| 6 |
-
<title>My static Space</title>
|
| 7 |
-
<link rel="stylesheet" href="style.css" />
|
| 8 |
-
</head>
|
| 9 |
-
<body>
|
| 10 |
-
<div class="card">
|
| 11 |
-
<h1>Welcome to your static Space!</h1>
|
| 12 |
-
<p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
|
| 13 |
-
<p>
|
| 14 |
-
Also don't forget to check the
|
| 15 |
-
<a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
|
| 16 |
-
</p>
|
| 17 |
-
</div>
|
| 18 |
-
</body>
|
| 19 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiohappyeyeballs==2.4.4
|
| 2 |
+
aiohttp==3.11.10
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
annotated-types==0.7.0
|
| 5 |
+
anyio==4.7.0
|
| 6 |
+
asgiref==3.8.1
|
| 7 |
+
attrs==24.2.0
|
| 8 |
+
backoff==2.2.1
|
| 9 |
+
bcrypt==4.2.1
|
| 10 |
+
build==1.2.2.post1
|
| 11 |
+
cachetools==5.5.0
|
| 12 |
+
certifi==2024.8.30
|
| 13 |
+
charset-normalizer==3.4.0
|
| 14 |
+
chroma-hnswlib==0.7.6
|
| 15 |
+
chromadb==0.5.23
|
| 16 |
+
click==8.1.7
|
| 17 |
+
colorama==0.4.6
|
| 18 |
+
coloredlogs==15.0.1
|
| 19 |
+
dataclasses-json==0.6.7
|
| 20 |
+
Deprecated==1.2.15
|
| 21 |
+
distro==1.9.0
|
| 22 |
+
durationpy==0.9
|
| 23 |
+
fastapi==0.115.6
|
| 24 |
+
filelock==3.16.1
|
| 25 |
+
flatbuffers==24.3.25
|
| 26 |
+
frozenlist==1.5.0
|
| 27 |
+
fsspec==2024.10.0
|
| 28 |
+
google-auth==2.37.0
|
| 29 |
+
googleapis-common-protos==1.66.0
|
| 30 |
+
greenlet==3.1.1
|
| 31 |
+
groq==0.13.0
|
| 32 |
+
grpcio==1.68.1
|
| 33 |
+
h11==0.14.0
|
| 34 |
+
httpcore==1.0.7
|
| 35 |
+
httptools==0.6.4
|
| 36 |
+
httpx==0.27.2
|
| 37 |
+
httpx-sse==0.4.0
|
| 38 |
+
huggingface-hub==0.26.5
|
| 39 |
+
humanfriendly==10.0
|
| 40 |
+
idna==3.10
|
| 41 |
+
importlib_metadata==8.5.0
|
| 42 |
+
importlib_resources==6.4.5
|
| 43 |
+
Jinja2==3.1.4
|
| 44 |
+
joblib==1.4.2
|
| 45 |
+
jsonpatch==1.33
|
| 46 |
+
jsonpointer==3.0.0
|
| 47 |
+
kubernetes==31.0.0
|
| 48 |
+
langchain==0.3.11
|
| 49 |
+
langchain-community==0.3.11
|
| 50 |
+
langchain-core==0.3.24
|
| 51 |
+
langchain-groq==0.2.1
|
| 52 |
+
langchain-huggingface==0.1.2
|
| 53 |
+
langchain-text-splitters==0.3.2
|
| 54 |
+
langserve==0.3.0
|
| 55 |
+
langsmith==0.2.3
|
| 56 |
+
lxml==5.3.0
|
| 57 |
+
markdown-it-py==3.0.0
|
| 58 |
+
MarkupSafe==3.0.2
|
| 59 |
+
marshmallow==3.23.1
|
| 60 |
+
mdurl==0.1.2
|
| 61 |
+
mmh3==5.0.1
|
| 62 |
+
monotonic==1.6
|
| 63 |
+
mpmath==1.3.0
|
| 64 |
+
multidict==6.1.0
|
| 65 |
+
mypy-extensions==1.0.0
|
| 66 |
+
networkx==3.4.2
|
| 67 |
+
numpy==1.26.4
|
| 68 |
+
oauthlib==3.2.2
|
| 69 |
+
onnxruntime==1.20.1
|
| 70 |
+
opentelemetry-api==1.29.0
|
| 71 |
+
opentelemetry-exporter-otlp-proto-common==1.29.0
|
| 72 |
+
opentelemetry-exporter-otlp-proto-grpc==1.29.0
|
| 73 |
+
opentelemetry-instrumentation==0.50b0
|
| 74 |
+
opentelemetry-instrumentation-asgi==0.50b0
|
| 75 |
+
opentelemetry-instrumentation-fastapi==0.50b0
|
| 76 |
+
opentelemetry-proto==1.29.0
|
| 77 |
+
opentelemetry-sdk==1.29.0
|
| 78 |
+
opentelemetry-semantic-conventions==0.50b0
|
| 79 |
+
opentelemetry-util-http==0.50b0
|
| 80 |
+
orjson==3.10.12
|
| 81 |
+
overrides==7.7.0
|
| 82 |
+
packaging==24.2
|
| 83 |
+
pillow==11.0.0
|
| 84 |
+
posthog==3.7.4
|
| 85 |
+
propcache==0.2.1
|
| 86 |
+
protobuf==5.29.1
|
| 87 |
+
pyasn1==0.6.1
|
| 88 |
+
pyasn1_modules==0.4.1
|
| 89 |
+
pydantic==2.10.3
|
| 90 |
+
pydantic-settings==2.6.1
|
| 91 |
+
pydantic_core==2.27.1
|
| 92 |
+
Pygments==2.18.0
|
| 93 |
+
PyPDF2==3.0.1
|
| 94 |
+
PyPika==0.48.9
|
| 95 |
+
pyproject_hooks==1.2.0
|
| 96 |
+
pyreadline3==3.5.4
|
| 97 |
+
python-dateutil==2.9.0.post0
|
| 98 |
+
python-docx==1.1.2
|
| 99 |
+
python-dotenv==1.0.1
|
| 100 |
+
python-multipart==0.0.19
|
| 101 |
+
PyYAML==6.0.2
|
| 102 |
+
regex==2024.11.6
|
| 103 |
+
requests==2.32.3
|
| 104 |
+
requests-oauthlib==2.0.0
|
| 105 |
+
requests-toolbelt==1.0.0
|
| 106 |
+
rich==13.9.4
|
| 107 |
+
rsa==4.9
|
| 108 |
+
safetensors==0.4.5
|
| 109 |
+
scikit-learn==1.6.0
|
| 110 |
+
scipy==1.14.1
|
| 111 |
+
sentence-transformers==3.3.1
|
| 112 |
+
shellingham==1.5.4
|
| 113 |
+
six==1.17.0
|
| 114 |
+
sniffio==1.3.1
|
| 115 |
+
SQLAlchemy==2.0.36
|
| 116 |
+
sse-starlette==2.1.3
|
| 117 |
+
starlette==0.41.3
|
| 118 |
+
sympy==1.13.1
|
| 119 |
+
tenacity==9.0.0
|
| 120 |
+
threadpoolctl==3.5.0
|
| 121 |
+
tokenizers==0.21.0
|
| 122 |
+
torch==2.5.1
|
| 123 |
+
tqdm==4.67.1
|
| 124 |
+
transformers==4.47.0
|
| 125 |
+
typer==0.15.1
|
| 126 |
+
typing-inspect==0.9.0
|
| 127 |
+
typing_extensions==4.12.2
|
| 128 |
+
urllib3==2.2.3
|
| 129 |
+
uvicorn==0.32.1
|
| 130 |
+
watchfiles==1.0.3
|
| 131 |
+
websocket-client==1.8.0
|
| 132 |
+
websockets==14.1
|
| 133 |
+
wrapt==1.17.0
|
| 134 |
+
yarl==1.18.3
|
| 135 |
+
zipp==3.21.0
|
static/index.html
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Chat and File Upload with Markdown</title>
|
| 7 |
+
<link rel="stylesheet" href="styles.css">
|
| 8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
| 9 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css">
|
| 10 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/13.0.1/markdown-it.min.js"></script>
|
| 11 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
|
| 12 |
+
</head>
|
| 13 |
+
<body>
|
| 14 |
+
<div class="chat-container">
|
| 15 |
+
<div class="chat-header">
|
| 16 |
+
Chat and File Upload with Markdown
|
| 17 |
+
</div>
|
| 18 |
+
<div class="chat-content" id="chat-content"></div>
|
| 19 |
+
<div class="chat-footer">
|
| 20 |
+
<div class="file-upload">
|
| 21 |
+
<input type="file" id="file-input" multiple>
|
| 22 |
+
<label for="file-input"><i class="fas fa-upload"></i> Upload File</label>
|
| 23 |
+
</div>
|
| 24 |
+
<input type="text" id="question" placeholder="Type your message here...">
|
| 25 |
+
<button id="send-btn">Send</button>
|
| 26 |
+
</div>
|
| 27 |
+
</div>
|
| 28 |
+
<script src="script.js"></script>
|
| 29 |
+
</body>
|
| 30 |
+
</html>
|
static/script.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const chatContent = document.getElementById('chat-content');
|
| 2 |
+
const questionInput = document.getElementById('question');
|
| 3 |
+
const sendButton = document.getElementById('send-btn');
|
| 4 |
+
const fileInput = document.getElementById('file-input');
|
| 5 |
+
|
| 6 |
+
// Initialize Markdown-it with syntax highlighting
|
| 7 |
+
const md = markdownit({
|
| 8 |
+
highlight: function (str, lang) {
|
| 9 |
+
if (lang && hljs.getLanguage(lang)) {
|
| 10 |
+
try {
|
| 11 |
+
return '<pre><code class="hljs">' + hljs.highlight(str, { language: lang }).value + '</code></pre>';
|
| 12 |
+
} catch (__) {}
|
| 13 |
+
}
|
| 14 |
+
return '<pre><code>' + md.utils.escapeHtml(str) + '</code></pre>';
|
| 15 |
+
},
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
// Function to add messages to the chat
|
| 19 |
+
const addMessage = (text, type, isMarkdown = false) => {
|
| 20 |
+
const messageDiv = document.createElement('div');
|
| 21 |
+
messageDiv.className = `message ${type}`;
|
| 22 |
+
const messageContent = document.createElement('p');
|
| 23 |
+
messageContent.innerHTML = isMarkdown ? md.render(text) : text;
|
| 24 |
+
messageDiv.appendChild(messageContent);
|
| 25 |
+
chatContent.appendChild(messageDiv);
|
| 26 |
+
chatContent.scrollTop = chatContent.scrollHeight;
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
// Handle Send Button Click for Chat Messages
|
| 30 |
+
sendButton.addEventListener('click', async () => {
|
| 31 |
+
const question = questionInput.value.trim();
|
| 32 |
+
if (!question) return;
|
| 33 |
+
|
| 34 |
+
// Add user's message to the chat
|
| 35 |
+
addMessage(question, 'user');
|
| 36 |
+
|
| 37 |
+
// Clear the input field
|
| 38 |
+
questionInput.value = '';
|
| 39 |
+
|
| 40 |
+
// Send the question to the backend
|
| 41 |
+
try {
|
| 42 |
+
const response = await fetch('http://127.0.0.1:8080/qa', {
|
| 43 |
+
method: 'POST',
|
| 44 |
+
headers: { 'Content-Type': 'application/json' },
|
| 45 |
+
body: JSON.stringify({ question }),
|
| 46 |
+
});
|
| 47 |
+
|
| 48 |
+
const data = await response.json();
|
| 49 |
+
|
| 50 |
+
// Add bot's response to the chat
|
| 51 |
+
if (data.answer) {
|
| 52 |
+
addMessage(data.answer, 'bot', true); // Render bot's response as Markdown
|
| 53 |
+
} else if (data.error) {
|
| 54 |
+
addMessage(`Error: ${data.error}`, 'bot');
|
| 55 |
+
}
|
| 56 |
+
} catch (error) {
|
| 57 |
+
addMessage(`Error: ${error.message}`, 'bot');
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// Handle File Upload
|
| 62 |
+
fileInput.addEventListener('change', async (event) => {
|
| 63 |
+
const files = event.target.files;
|
| 64 |
+
|
| 65 |
+
if (files.length > 0) {
|
| 66 |
+
const formData = new FormData();
|
| 67 |
+
Array.from(files).forEach((file) => {
|
| 68 |
+
formData.append('file', file);
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
// Send the file to the backend
|
| 72 |
+
try {
|
| 73 |
+
const response = await fetch('http://127.0.0.1:8080/upload', {
|
| 74 |
+
method: 'POST',
|
| 75 |
+
body: formData,
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
const data = await response.json();
|
| 79 |
+
|
| 80 |
+
// Add a confirmation message to the chat
|
| 81 |
+
if (data.message) {
|
| 82 |
+
addMessage(data.message, 'bot');
|
| 83 |
+
} else if (data.error) {
|
| 84 |
+
addMessage(`Error: ${data.error}`, 'bot');
|
| 85 |
+
}
|
| 86 |
+
} catch (error) {
|
| 87 |
+
addMessage(`Error: ${error.message}`, 'bot');
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
});
|
static/styles.css
ADDED
|
File without changes
|
style.css
DELETED
|
@@ -1,28 +0,0 @@
|
|
| 1 |
-
body {
|
| 2 |
-
padding: 2rem;
|
| 3 |
-
font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
|
| 4 |
-
}
|
| 5 |
-
|
| 6 |
-
h1 {
|
| 7 |
-
font-size: 16px;
|
| 8 |
-
margin-top: 0;
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
p {
|
| 12 |
-
color: rgb(107, 114, 128);
|
| 13 |
-
font-size: 15px;
|
| 14 |
-
margin-bottom: 10px;
|
| 15 |
-
margin-top: 5px;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
.card {
|
| 19 |
-
max-width: 620px;
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
.card p:last-child {
|
| 27 |
-
margin-bottom: 0;
|
| 28 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|