chatbot-sverige / app_simple.py
organicoder's picture
Update app_simple.py
f21bba6 verified
import gradio as gr
import openai
import os
import PyPDF2
import re
from typing import List, Tuple
# Configuration
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
MAX_TOKENS = int(os.getenv("MAX_TOKENS", "500"))
TEMPERATURE = float(os.getenv("TEMPERATURE", "0.7"))
# Validate API key
if not OPENAI_API_KEY:
print("❌ OPENAI_API_KEY environment variable is required")
exit(1)
# Initialize OpenAI client
client = openai.OpenAI(api_key=OPENAI_API_KEY)
# Simple PDF processing
class SimplePDFHelper:
def __init__(self, pdf_path="Health Tech Hub Copenhagen.pdf"):
self.pdf_path = pdf_path
self.pdf_text = ""
self.loaded = False
def load_pdf(self):
"""Load and extract text from PDF"""
if self.loaded:
return True
try:
if not os.path.exists(self.pdf_path):
print(f"⚠️ PDF file not found: {self.pdf_path}")
return False
text = ""
with open(self.pdf_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
self.pdf_text = text
self.loaded = True
print(f"✅ PDF loaded successfully ({len(text)} characters)")
return True
except Exception as e:
print(f"❌ Error loading PDF: {e}")
return False
def search_text(self, query: str, max_length: int = 500) -> str:
"""Simple text search in PDF content"""
if not self.loaded:
if not self.load_pdf():
return ""
# Simple keyword search
query_words = set(re.findall(r'\b\w+\b', query.lower()))
# Split text into sentences
sentences = re.split(r'[.!?]+', self.pdf_text)
# Find sentences with matching keywords
relevant_sentences = []
for sentence in sentences:
sentence_words = set(re.findall(r'\b\w+\b', sentence.lower()))
if query_words.intersection(sentence_words):
relevant_sentences.append(sentence.strip())
# Return first few relevant sentences
if relevant_sentences:
result = ". ".join(relevant_sentences[:3]) + "."
return result[:max_length]
return ""
# Initialize PDF helper
pdf_helper = SimplePDFHelper()
def chat_with_bot(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
"""Chat function with simple PDF integration"""
if not message.strip():
return "", history
# Search for relevant PDF content
pdf_context = pdf_helper.search_text(message)
# Prepare system message
system_message = (
"You are a helpful AI assistant with knowledge about Health Tech Hub Copenhagen. "
"Use the provided PDF information when relevant to answer questions accurately. "
"Keep your responses concise and engaging."
)
# Prepare conversation history
messages = [{"role": "system", "content": system_message}]
# Add conversation history
for human, assistant in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
# Add current message with PDF context
full_message = message
if pdf_context:
full_message = f"{message}\n\nRelevant information from the Health Tech Hub Copenhagen document:\n{pdf_context}"
messages.append({"role": "user", "content": full_message})
try:
# Get response from OpenAI
response = client.chat.completions.create(
model=OPENAI_MODEL,
messages=messages,
max_tokens=MAX_TOKENS,
temperature=TEMPERATURE
)
assistant_response = response.choices[0].message.content
# Update history
history.append((message, assistant_response))
return "", history
except Exception as e:
error_message = f"Sorry, I encountered an error: {str(e)}"
history.append((message, error_message))
return "", history
def clear_chat():
"""Clear the chat history"""
return []
# Create Gradio interface
with gr.Blocks(
title="AI Chatbot with PDF Knowledge",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 800px;
margin: auto;
}
"""
) as demo:
gr.Markdown(
"""
Chatbot overskrift
"""
)
# Chat interface
chatbot = gr.Chatbot(
height=500,
show_label=False,
container=True,
bubble_full_width=False
)
# Message input
msg = gr.Textbox(
placeholder="Type your message here...",
show_label=False,
container=False
)
# Clear button
clear = gr.Button("Clear Chat", variant="secondary")
# Set up event handlers
msg.submit(
chat_with_bot,
inputs=[msg, chatbot],
outputs=[msg, chatbot]
)
clear.click(
clear_chat,
outputs=chatbot
)
# Launch the app
if __name__ == "__main__":
demo.launch()