File size: 5,400 Bytes
99a8155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f21bba6
 
99a8155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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()