organicoder commited on
Commit
99a8155
·
verified ·
1 Parent(s): 3f6db5c

Upload 4 files

Browse files
Files changed (3) hide show
  1. README.md +6 -6
  2. app_simple.py +192 -0
  3. requirements.txt +1 -3
README.md CHANGED
@@ -4,8 +4,8 @@ emoji: 🤖
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.36.2
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
@@ -51,7 +51,7 @@ export OPENAI_API_KEY="your-api-key-here"
51
 
52
  4. Run the application:
53
  ```bash
54
- python app.py
55
  ```
56
 
57
  The chatbot will be available at `http://localhost:7860`
@@ -76,7 +76,7 @@ git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
76
 
77
  3. Copy your files to the Space repository:
78
  ```bash
79
- cp app.py requirements.txt README.md /path/to/space/repo/
80
  ```
81
 
82
  4. Push to Hugging Face:
@@ -95,7 +95,7 @@ Make sure to set the following environment variable in your Hugging Face Space:
95
 
96
  ## Configuration
97
 
98
- You can customize the chatbot by modifying the following parameters in `app.py`:
99
 
100
  - **Model**: Change `gpt-3.5-turbo` to other OpenAI models
101
  - **Max tokens**: Adjust `max_tokens` for response length
@@ -121,4 +121,4 @@ This project is open source and available under the MIT License.
121
 
122
  ## Contributing
123
 
124
- 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_simple.py
9
  pinned: false
10
  ---
11
 
 
51
 
52
  4. Run the application:
53
  ```bash
54
+ python app_simple.py
55
  ```
56
 
57
  The chatbot will be available at `http://localhost:7860`
 
76
 
77
  3. Copy your files to the Space repository:
78
  ```bash
79
+ cp app_simple.py requirements.txt README.md "Health Tech Hub Copenhagen.pdf" /path/to/space/repo/
80
  ```
81
 
82
  4. Push to Hugging Face:
 
95
 
96
  ## Configuration
97
 
98
+ You can customize the chatbot by modifying the following parameters in `app_simple.py`:
99
 
100
  - **Model**: Change `gpt-3.5-turbo` to other OpenAI models
101
  - **Max tokens**: Adjust `max_tokens` for response length
 
121
 
122
  ## Contributing
123
 
124
+ Feel free to submit issues and enhancement requests!
app_simple.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ import PyPDF2
5
+ import re
6
+ from typing import List, Tuple
7
+
8
+ # Configuration
9
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
10
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
11
+ MAX_TOKENS = int(os.getenv("MAX_TOKENS", "500"))
12
+ TEMPERATURE = float(os.getenv("TEMPERATURE", "0.7"))
13
+
14
+ # Validate API key
15
+ if not OPENAI_API_KEY:
16
+ print("❌ OPENAI_API_KEY environment variable is required")
17
+ exit(1)
18
+
19
+ # Initialize OpenAI client
20
+ client = openai.OpenAI(api_key=OPENAI_API_KEY)
21
+
22
+ # Simple PDF processing
23
+ class SimplePDFHelper:
24
+ def __init__(self, pdf_path="Health Tech Hub Copenhagen.pdf"):
25
+ self.pdf_path = pdf_path
26
+ self.pdf_text = ""
27
+ self.loaded = False
28
+
29
+ def load_pdf(self):
30
+ """Load and extract text from PDF"""
31
+ if self.loaded:
32
+ return True
33
+
34
+ try:
35
+ if not os.path.exists(self.pdf_path):
36
+ print(f"⚠️ PDF file not found: {self.pdf_path}")
37
+ return False
38
+
39
+ text = ""
40
+ with open(self.pdf_path, 'rb') as file:
41
+ pdf_reader = PyPDF2.PdfReader(file)
42
+ for page in pdf_reader.pages:
43
+ text += page.extract_text() + "\n"
44
+
45
+ self.pdf_text = text
46
+ self.loaded = True
47
+ print(f"✅ PDF loaded successfully ({len(text)} characters)")
48
+ return True
49
+
50
+ except Exception as e:
51
+ print(f"❌ Error loading PDF: {e}")
52
+ return False
53
+
54
+ def search_text(self, query: str, max_length: int = 500) -> str:
55
+ """Simple text search in PDF content"""
56
+ if not self.loaded:
57
+ if not self.load_pdf():
58
+ return ""
59
+
60
+ # Simple keyword search
61
+ query_words = set(re.findall(r'\b\w+\b', query.lower()))
62
+
63
+ # Split text into sentences
64
+ sentences = re.split(r'[.!?]+', self.pdf_text)
65
+
66
+ # Find sentences with matching keywords
67
+ relevant_sentences = []
68
+ for sentence in sentences:
69
+ sentence_words = set(re.findall(r'\b\w+\b', sentence.lower()))
70
+ if query_words.intersection(sentence_words):
71
+ relevant_sentences.append(sentence.strip())
72
+
73
+ # Return first few relevant sentences
74
+ if relevant_sentences:
75
+ result = ". ".join(relevant_sentences[:3]) + "."
76
+ return result[:max_length]
77
+
78
+ return ""
79
+
80
+ # Initialize PDF helper
81
+ pdf_helper = SimplePDFHelper()
82
+
83
+ def chat_with_bot(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
84
+ """Chat function with simple PDF integration"""
85
+ if not message.strip():
86
+ return "", history
87
+
88
+ # Search for relevant PDF content
89
+ pdf_context = pdf_helper.search_text(message)
90
+
91
+ # Prepare system message
92
+ system_message = (
93
+ "You are a helpful AI assistant with knowledge about Health Tech Hub Copenhagen. "
94
+ "Use the provided PDF information when relevant to answer questions accurately. "
95
+ "Keep your responses concise and engaging."
96
+ )
97
+
98
+ # Prepare conversation history
99
+ messages = [{"role": "system", "content": system_message}]
100
+
101
+ # Add conversation history
102
+ for human, assistant in history:
103
+ messages.append({"role": "user", "content": human})
104
+ messages.append({"role": "assistant", "content": assistant})
105
+
106
+ # Add current message with PDF context
107
+ full_message = message
108
+ if pdf_context:
109
+ full_message = f"{message}\n\nRelevant information from the Health Tech Hub Copenhagen document:\n{pdf_context}"
110
+
111
+ messages.append({"role": "user", "content": full_message})
112
+
113
+ try:
114
+ # Get response from OpenAI
115
+ response = client.chat.completions.create(
116
+ model=OPENAI_MODEL,
117
+ messages=messages,
118
+ max_tokens=MAX_TOKENS,
119
+ temperature=TEMPERATURE
120
+ )
121
+
122
+ assistant_response = response.choices[0].message.content
123
+
124
+ # Update history
125
+ history.append((message, assistant_response))
126
+
127
+ return "", history
128
+
129
+ except Exception as e:
130
+ error_message = f"Sorry, I encountered an error: {str(e)}"
131
+ history.append((message, error_message))
132
+ return "", history
133
+
134
+ def clear_chat():
135
+ """Clear the chat history"""
136
+ return []
137
+
138
+ # Create Gradio interface
139
+ with gr.Blocks(
140
+ title="AI Chatbot with PDF Knowledge",
141
+ theme=gr.themes.Soft(),
142
+ css="""
143
+ .gradio-container {
144
+ max-width: 800px;
145
+ margin: auto;
146
+ }
147
+ """
148
+ ) as demo:
149
+ gr.Markdown(
150
+ """
151
+ # 🤖 AI Chatbot with PDF Knowledge
152
+
153
+ Welcome! I'm your AI assistant with knowledge about Health Tech Hub Copenhagen.
154
+ I can answer questions based on the PDF document and provide helpful information!
155
+
156
+ ---
157
+ """
158
+ )
159
+
160
+ # Chat interface
161
+ chatbot = gr.Chatbot(
162
+ height=500,
163
+ show_label=False,
164
+ container=True,
165
+ bubble_full_width=False
166
+ )
167
+
168
+ # Message input
169
+ msg = gr.Textbox(
170
+ placeholder="Type your message here...",
171
+ show_label=False,
172
+ container=False
173
+ )
174
+
175
+ # Clear button
176
+ clear = gr.Button("Clear Chat", variant="secondary")
177
+
178
+ # Set up event handlers
179
+ msg.submit(
180
+ chat_with_bot,
181
+ inputs=[msg, chatbot],
182
+ outputs=[msg, chatbot]
183
+ )
184
+
185
+ clear.click(
186
+ clear_chat,
187
+ outputs=chatbot
188
+ )
189
+
190
+ # Launch the app
191
+ if __name__ == "__main__":
192
+ demo.launch()
requirements.txt CHANGED
@@ -1,6 +1,4 @@
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
 
1
  gradio>=4.0.0
2
  openai>=1.0.0
3
  python-dotenv>=1.0.0
4
+ PyPDF2>=3.0.0