Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .gitattributes +1 -0
- Health Tech Hub Copenhagen.pdf +3 -0
- README.md +6 -4
- app.py +30 -4
- config.py +1 -1
- requirements.txt +4 -1
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Health[[:space:]]Tech[[:space:]]Hub[[:space:]]Copenhagen.pdf filter=lfs diff=lfs merge=lfs -text
|
Health Tech Hub Copenhagen.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:56203166dfcbf7b401ca8900a85a7baf2a9428550d886fb81937e7b1284d9188
|
| 3 |
+
size 107629
|
README.md
CHANGED
|
@@ -4,18 +4,20 @@ emoji: π€
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# AI Chatbot
|
| 13 |
|
| 14 |
-
A
|
| 15 |
|
| 16 |
## Features
|
| 17 |
|
| 18 |
- π€ Powered by OpenAI's GPT-3.5-turbo
|
|
|
|
|
|
|
| 19 |
- π¬ Real-time chat interface
|
| 20 |
- π¨ Beautiful and responsive UI
|
| 21 |
- π± Mobile-friendly design
|
|
@@ -119,4 +121,4 @@ This project is open source and available under the MIT License.
|
|
| 119 |
|
| 120 |
## Contributing
|
| 121 |
|
| 122 |
-
Feel free to submit issues and enhancement requests!
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# AI Chatbot with PDF Knowledge
|
| 13 |
|
| 14 |
+
A smart AI chatbot built with Gradio and OpenAI's GPT models, enhanced with PDF document knowledge. The chatbot can answer questions based on the Health Tech Hub Copenhagen PDF document, making it perfect for health tech inquiries and information retrieval.
|
| 15 |
|
| 16 |
## Features
|
| 17 |
|
| 18 |
- π€ Powered by OpenAI's GPT-3.5-turbo
|
| 19 |
+
- π PDF Knowledge Integration - Answers based on Health Tech Hub Copenhagen document
|
| 20 |
+
- π Semantic Search - Finds relevant information from the PDF
|
| 21 |
- π¬ Real-time chat interface
|
| 22 |
- π¨ Beautiful and responsive UI
|
| 23 |
- π± Mobile-friendly design
|
|
|
|
| 121 |
|
| 122 |
## Contributing
|
| 123 |
|
| 124 |
+
Feel free to submit issues and enhancement requests!
|
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import openai
|
|
| 3 |
import os
|
| 4 |
from typing import List, Tuple
|
| 5 |
from config import Config
|
|
|
|
| 6 |
|
| 7 |
# Validate configuration
|
| 8 |
try:
|
|
@@ -16,16 +17,37 @@ client = openai.OpenAI(
|
|
| 16 |
api_key=Config.OPENAI_API_KEY
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def chat_with_bot(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
| 20 |
"""
|
| 21 |
-
Chat function that handles conversation with OpenAI API
|
| 22 |
"""
|
| 23 |
if not message.strip():
|
| 24 |
return "", history
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Prepare conversation history for OpenAI
|
| 27 |
messages = [
|
| 28 |
-
{"role": "system", "content": Config.SYSTEM_PROMPT}
|
| 29 |
]
|
| 30 |
|
| 31 |
# Add conversation history
|
|
@@ -33,8 +55,12 @@ def chat_with_bot(message: str, history: List[Tuple[str, str]]) -> Tuple[str, Li
|
|
| 33 |
messages.append({"role": "user", "content": human})
|
| 34 |
messages.append({"role": "assistant", "content": assistant})
|
| 35 |
|
| 36 |
-
# Add current message
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
# Get response from OpenAI
|
|
|
|
| 3 |
import os
|
| 4 |
from typing import List, Tuple
|
| 5 |
from config import Config
|
| 6 |
+
from pdf_processor import PDFProcessor
|
| 7 |
|
| 8 |
# Validate configuration
|
| 9 |
try:
|
|
|
|
| 17 |
api_key=Config.OPENAI_API_KEY
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Initialize PDF processor
|
| 21 |
+
pdf_processor = PDFProcessor()
|
| 22 |
+
|
| 23 |
+
# Try to load existing vector store, otherwise process PDF
|
| 24 |
+
if not pdf_processor.load_vector_store():
|
| 25 |
+
print("π Processing PDF for the first time...")
|
| 26 |
+
if pdf_processor.process_pdf():
|
| 27 |
+
pdf_processor.save_vector_store()
|
| 28 |
+
else:
|
| 29 |
+
print("β οΈ PDF processing failed. Chatbot will work without PDF knowledge.")
|
| 30 |
+
|
| 31 |
def chat_with_bot(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
| 32 |
"""
|
| 33 |
+
Chat function that handles conversation with OpenAI API and PDF knowledge
|
| 34 |
"""
|
| 35 |
if not message.strip():
|
| 36 |
return "", history
|
| 37 |
|
| 38 |
+
# Search for relevant PDF content
|
| 39 |
+
pdf_context = ""
|
| 40 |
+
try:
|
| 41 |
+
if pdf_processor.vector_store:
|
| 42 |
+
relevant_chunks = pdf_processor.search_similar_content(message, k=2)
|
| 43 |
+
if relevant_chunks:
|
| 44 |
+
pdf_context = "\n\nRelevant information from the Health Tech Hub Copenhagen document:\n" + "\n".join(relevant_chunks)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Warning: Could not search PDF content: {e}")
|
| 47 |
+
|
| 48 |
# Prepare conversation history for OpenAI
|
| 49 |
messages = [
|
| 50 |
+
{"role": "system", "content": Config.SYSTEM_PROMPT + "\n\nYou have access to information about Health Tech Hub Copenhagen. Use this information when relevant to answer questions."}
|
| 51 |
]
|
| 52 |
|
| 53 |
# Add conversation history
|
|
|
|
| 55 |
messages.append({"role": "user", "content": human})
|
| 56 |
messages.append({"role": "assistant", "content": assistant})
|
| 57 |
|
| 58 |
+
# Add current message with PDF context
|
| 59 |
+
full_message = message
|
| 60 |
+
if pdf_context:
|
| 61 |
+
full_message = f"{message}\n\n{pdf_context}"
|
| 62 |
+
|
| 63 |
+
messages.append({"role": "user", "content": full_message})
|
| 64 |
|
| 65 |
try:
|
| 66 |
# Get response from OpenAI
|
config.py
CHANGED
|
@@ -13,7 +13,7 @@ class Config:
|
|
| 13 |
# System prompt
|
| 14 |
SYSTEM_PROMPT: str = os.getenv(
|
| 15 |
"SYSTEM_PROMPT",
|
| 16 |
-
"You are a helpful and friendly AI assistant. Keep your responses concise and engaging."
|
| 17 |
)
|
| 18 |
|
| 19 |
# Gradio Configuration
|
|
|
|
| 13 |
# System prompt
|
| 14 |
SYSTEM_PROMPT: str = os.getenv(
|
| 15 |
"SYSTEM_PROMPT",
|
| 16 |
+
"You are a helpful and friendly AI assistant with knowledge about Health Tech Hub Copenhagen. Use the provided PDF information when relevant to answer questions accurately and comprehensively. Keep your responses concise and engaging."
|
| 17 |
)
|
| 18 |
|
| 19 |
# Gradio Configuration
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
openai>=1.0.0
|
| 3 |
-
python-dotenv>=1.0.0
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
openai>=1.0.0
|
| 3 |
+
python-dotenv>=1.0.0
|
| 4 |
+
PyPDF2>=3.0.0
|
| 5 |
+
langchain>=0.1.0
|
| 6 |
+
langchain-openai>=0.1.0
|