File size: 10,311 Bytes
d26fbaa
7d0b369
 
d26fbaa
7d0b369
 
 
 
 
 
 
 
 
 
ef010f9
 
 
7d0b369
 
 
 
 
 
 
 
 
5afbf19
 
7d0b369
 
 
 
 
 
 
d26fbaa
1259c02
 
d26fbaa
1022bd2
d26fbaa
 
1022bd2
faab237
 
 
 
7d0b369
1022bd2
faab237
63950f4
fb35dd4
491a482
faab237
8dbfa9b
 
faab237
5afbf19
 
 
faab237
 
63950f4
8dbfa9b
faab237
 
 
 
 
1022bd2
8dbfa9b
 
fb35dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faab237
999a470
2b375a8
1259c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1022bd2
1259c02
 
 
 
1022bd2
 
1259c02
 
 
 
1022bd2
1259c02
 
1022bd2
1259c02
 
 
 
1022bd2
1259c02
 
5b3c21e
d26fbaa
5afbf19
 
 
 
d26fbaa
 
 
 
 
 
1022bd2
 
 
 
 
 
 
d26fbaa
1022bd2
999a470
 
 
 
 
 
 
 
5b3c21e
d26fbaa
1022bd2
d26fbaa
5afbf19
 
 
 
1022bd2
faab237
d26fbaa
fb35dd4
1022bd2
d26fbaa
 
1022bd2
 
d26fbaa
 
5b3c21e
d26fbaa
 
 
 
 
1022bd2
 
d26fbaa
 
 
 
1022bd2
 
 
 
 
 
 
d26fbaa
 
1022bd2
d26fbaa
 
8dbfa9b
5afbf19
 
8dbfa9b
5afbf19
 
 
8dbfa9b
 
 
 
 
 
d26fbaa
1022bd2
 
5afbf19
 
 
 
 
 
 
 
 
 
 
 
 
d26fbaa
1259c02
1022bd2
 
1259c02
 
 
 
1022bd2
 
 
 
1259c02
 
 
 
 
1022bd2
1259c02
 
1022bd2
1259c02
 
1022bd2
1259c02
1022bd2
 
1259c02
5b3c21e
 
1259c02
 
 
fda3f46
5b3c21e
 
 
 
fda3f46
 
 
 
 
 
 
5b3c21e
fda3f46
5b3c21e
1259c02
 
 
1022bd2
 
 
1259c02
1022bd2
 
1259c02
1022bd2
 
d26fbaa
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# app.py
import subprocess
import sys
import os

# Run installation commands at startup
def install_packages():
    print("Starting package installation...")
    
    # Upgrade pip
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
    
    # Install compatible versions in a specific order
    subprocess.check_call([sys.executable, "-m", "pip", "install", "websockets==10.4"])
    # Update both gradio and gradio-client to compatible versions
    subprocess.check_call([sys.executable, "-m", "pip", "install", "gradio==3.44.4"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "gradio-client==0.6.1"])
    
    # Install the rest of the requirements
    subprocess.check_call([sys.executable, "-m", "pip", "install", "PyPDF2==3.0.1"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "langchain==0.0.340"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "faiss-cpu==1.7.4"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "sentence-transformers==2.3.0"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "zhipuai>=2.1.0"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "transformers==4.35.2"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "torch==2.1.0"])
    # Updated huggingface-hub version to resolve dependency conflict
    subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface-hub==0.24.0"])
    
    print("Package installation completed successfully")

# Run the installation
install_packages()

# Now continue with the rest of the app
import gradio as gr
import sqlite3
from datetime import datetime
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from langchain.llms.base import LLM
from typing import Optional, List, Dict, Any
from zhipuai import ZhipuAI

# Custom LLM wrapper for Zhipu AI
class ZhipuAILLM(LLM):
    api_key: str
    # Updated model name to a more commonly available one
    model: str = "glm-4-flash"  # Changed from "chatglm3-6b"
    temperature: float = 0.1
    # Declare client as a field to avoid Pydantic validation error
    client: Optional[ZhipuAI] = None

    def __init__(self, api_key: str, **kwargs: Any):
        # Pass api_key to parent class
        super().__init__(api_key=api_key, **kwargs)
        self.model = kwargs.get("model", self.model)
        self.temperature = kwargs.get("temperature", self.temperature)
        # Initialize client after setting attributes
        self.client = ZhipuAI(api_key=self.api_key)

    @property
    def _llm_type(self) -> str:
        return "zhipuai"

    def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any) -> str:
        if self.client is None:
            raise ValueError("ZhipuAI client not initialized")
        
        try:
            response = self.client.chat.completions.create(
                model=self.model,
                messages=[{"role": "user", "content": prompt}],
                temperature=self.temperature
            )
            return response.choices[0].message.content
        except Exception as e:
            # Handle API errors gracefully
            error_msg = str(e)
            if "403" in error_msg:
                return "I apologize, but I'm currently unable to access the language model. This could be due to API access restrictions. Please check your API key and model permissions."
            elif "429" in error_msg:
                return "I'm experiencing high demand right now. Please try again in a moment."
            else:
                return f"An error occurred: {error_msg}"

# Database setup
DB_PATH = "chat_history.db"

def init_db():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS chat_history (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        timestamp TEXT NOT NULL,
        user_message TEXT NOT NULL,
        bot_response TEXT NOT NULL
    )
    ''')
    conn.commit()
    conn.close()

def save_chat(user_message, bot_response):
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    cursor.execute(
        "INSERT INTO chat_history (timestamp, user_message, bot_response) VALUES (?, ?, ?)",
        (timestamp, user_message, bot_response)
    )
    conn.commit()
    conn.close()

def get_chat_history():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("SELECT timestamp, user_message, bot_response FROM chat_history ORDER BY timestamp DESC")
    history = cursor.fetchall()
    conn.close()
    return history

# Initialize database
init_db()

# Initialize RAG system
def initialize_system(pdf_path):
    # Check if PDF file exists
    if not os.path.exists(pdf_path):
        raise FileNotFoundError(f"PDF file not found: {pdf_path}")
    
    # Extract text from PDF
    pdf_reader = PdfReader(pdf_path)
    text = ""
    for page in pdf_reader.pages:
        text += page.extract_text()
    
    # Split text into chunks
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len
    )
    chunks = text_splitter.split_text(text)
    
    # Create embeddings
    try:
        embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
    except Exception as e:
        print(f"Error with HuggingFaceEmbeddings: {e}")
        # Fallback to a different embedding method
        from langchain.embeddings import FakeEmbeddings
        embeddings = FakeEmbeddings(size=384)
        print("Using FakeEmbeddings as fallback")
    
    # Create vector store
    vector_store = FAISS.from_texts(chunks, embeddings)
    
    # Check if API key is available
    if "ZHIPU_API_KEY" not in os.environ:
        raise ValueError("ZHIPU_API_KEY environment variable is not set")
    
    # Initialize Zhipu LLM
    llm = ZhipuAILLM(
        api_key=os.environ["ZHIPU_API_KEY"],
        model="glm-4",  # Updated model name
        temperature=0.1
    )
    
    # Create prompt template
    prompt_template = """
    You are a personal avatar representing me. Answer the question based only on the provided context.
    If the information is not in the context, politely say you don't have that information.
    Always answer in first person as if you are me.
    
    Context: {context}
    Question: {question}
    
    Answer:
    """
    prompt = PromptTemplate(
        template=prompt_template,
        input_variables=["context", "question"]
    )
    
    # Create RAG chain
    qa_chain = RetrievalQA.from_chain_type(
        llm=llm,
        chain_type="stuff",
        retriever=vector_store.as_retriever(),
        chain_type_kwargs={"prompt": prompt},
        return_source_documents=True
    )
    
    return qa_chain

# Initialize on startup
qa_chain = None
try:
    qa_chain = initialize_system("Henry_Linkedin_Profile.pdf")
    print("System initialized successfully")
except Exception as e:
    print(f"Error initializing system: {e}")
    # Create a dummy chain to allow the app to run
    # Instead of using OpenAI, we'll create a simple dummy chain
    class DummyChain:
        def __call__(self, inputs):
            return {"result": f"System initialization failed: {str(e)}"}
    
    qa_chain = DummyChain()

# Chat function
def chat(message, history):
    try:
        result = qa_chain({"query": message})
        response = result["result"]
        formatted_response = f"{response}\n\n*(Information from your profile)*"
        
        # Save to database
        save_chat(message, formatted_response)
        
        return formatted_response
    except Exception as e:
        error_msg = f"Error processing your request: {str(e)}"
        save_chat(message, error_msg)
        return error_msg

# Function to display chat history
def display_history():
    history = get_chat_history()
    if not history:
        return "No chat history yet."
    
    formatted_history = []
    for timestamp, user_msg, bot_resp in history:
        formatted_history.append(f"**[{timestamp}]**")
        formatted_history.append(f"**You:** {user_msg}")
        formatted_history.append(f"**Avatar:** {bot_resp}")
        formatted_history.append("---")
    
    return "\n".join(formatted_history)

# Function to clear chat history
def clear_history():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("DELETE FROM chat_history")
    conn.commit()
    conn.close()
    return "Chat history cleared."

# Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# My Personal Avatar")
    gr.Markdown("Ask me anything about my background, skills, or experience!")
    
    with gr.Tabs():
        # Chat tab
        with gr.TabItem("Chat"):
            # Using a simpler chat interface
            chatbot = gr.Chatbot(height=500)
            msg = gr.Textbox(label="Your Question", placeholder="Type your question here...")
            clear = gr.Button("Clear Conversation")
            
            def respond(message, chat_history):
                if not message:
                    return "", chat_history
                
                bot_message = chat(message, chat_history)
                chat_history.append((message, bot_message))
                return "", chat_history
            
            msg.submit(respond, [msg, chatbot], [msg, chatbot])
            clear.click(lambda: None, None, chatbot, queue=False)
        
        # History tab
        with gr.TabItem("Chat History"):
            history_output = gr.Markdown()
            refresh_button = gr.Button("Refresh History")
            clear_button = gr.Button("Clear History")
            
            refresh_button.click(display_history, outputs=history_output)
            clear_button.click(clear_history, outputs=history_output)
            
            # Initialize history display
            demo.load(display_history, outputs=history_output)

if __name__ == "__main__":
    demo.launch()