ariankhalfani commited on
Commit
ee1461b
1 Parent(s): ea1b862

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sqlite3
3
+ import requests
4
+ import PyPDF2
5
+ import faiss
6
+ import numpy as np
7
+ from sentence_transformers import SentenceTransformer
8
+ import gradio as gr
9
+
10
+ # Configure Hugging Face API
11
+ huggingface_api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
12
+ huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
13
+ headers = {"Authorization": f"Bearer {huggingface_api_key}"}
14
+
15
+ # Function to query Hugging Face model
16
+ def query_huggingface(payload):
17
+ response = requests.post(huggingface_api_url, headers=headers, json=payload)
18
+ return response.json()
19
+
20
+ # Function to extract text from PDF
21
+ def extract_text_from_pdf(pdf_file):
22
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
23
+ text = ""
24
+ for page_num in range(len(pdf_reader.pages)):
25
+ page = pdf_reader.pages[page_num]
26
+ text += page.extract_text()
27
+ return text
28
+
29
+ # Initialize SQLite database
30
+ def init_db():
31
+ conn = sqlite3.connect('storage_warehouse.db')
32
+ c = conn.cursor()
33
+ c.execute('''
34
+ CREATE TABLE IF NOT EXISTS context (
35
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
36
+ name TEXT,
37
+ content TEXT
38
+ )
39
+ ''')
40
+ conn.commit()
41
+ conn.close()
42
+
43
+ # Add context to the database
44
+ def add_context(name, content):
45
+ conn = sqlite3.connect('storage_warehouse.db')
46
+ c = conn.cursor()
47
+ c.execute('INSERT INTO context (name, content) VALUES (?, ?)', (name, content))
48
+ conn.commit()
49
+ conn.close()
50
+
51
+ # Retrieve context from the database
52
+ def get_context():
53
+ conn = sqlite3.connect('storage_warehouse.db')
54
+ c = conn.cursor()
55
+ c.execute('SELECT content FROM context')
56
+ context = c.fetchall()
57
+ conn.close()
58
+ return [c[0] for c in context]
59
+
60
+ # Function to create or update the FAISS index
61
+ def update_faiss_index():
62
+ contexts = get_context()
63
+ embeddings = model.encode(contexts, convert_to_tensor=True)
64
+ index = faiss.IndexFlatL2(embeddings.shape[1])
65
+ index.add(embeddings.cpu().numpy())
66
+ return index, contexts
67
+
68
+ # Retrieve relevant context from the FAISS index
69
+ def retrieve_relevant_context(index, contexts, query, top_k=5):
70
+ query_embedding = model.encode([query], convert_to_tensor=True).cpu().numpy()
71
+ distances, indices = index.search(query_embedding, top_k)
72
+ relevant_contexts = [contexts[i] for i in indices[0]]
73
+ return relevant_contexts
74
+
75
+ # Initialize the database and FAISS model
76
+ init_db()
77
+ model = SentenceTransformer('all-MiniLM-L6-v2')
78
+ faiss_index, context_list = update_faiss_index()
79
+
80
+ # Function to handle chatbot responses
81
+ def chatbot_response(question):
82
+ relevant_contexts = retrieve_relevant_context(faiss_index, context_list, question)
83
+ user_input = f"question: {question} context: {' '.join(relevant_contexts)}"
84
+ response = query_huggingface({"inputs": user_input})
85
+ response_text = response.get("generated_text", "Sorry, I couldn't generate a response.")
86
+ return response_text
87
+
88
+ # Function to handle PDF uploads
89
+ def handle_pdf_upload(pdf_file):
90
+ context = extract_text_from_pdf(pdf_file)
91
+ add_context(pdf_file.name, context)
92
+ faiss_index, context_list = update_faiss_index() # Update FAISS index
93
+ return f"Context from {pdf_file.name} added to the database."
94
+
95
+ # Gradio UI
96
+ with gr.Blocks() as demo:
97
+ gr.Markdown("# Storage Warehouse Customer Service Chatbot")
98
+
99
+ with gr.Row():
100
+ with gr.Column(scale=4):
101
+ with gr.Box():
102
+ pdf_upload = gr.File(label="Upload PDF", file_types=["pdf"], interactive=True)
103
+ upload_button = gr.Button("Upload")
104
+ upload_status = gr.Textbox(label="Upload Status")
105
+
106
+ def handle_upload(files):
107
+ for file in files:
108
+ result = handle_pdf_upload(file.name)
109
+ upload_status.value = result
110
+
111
+ upload_button.click(fn=handle_upload, inputs=pdf_upload, outputs=upload_status)
112
+
113
+ with gr.Column(scale=8):
114
+ chatbot = gr.Chatbot(label="Chatbot")
115
+ question = gr.Textbox(label="Your question here:")
116
+ submit_button = gr.Button("Submit")
117
+
118
+ def handle_chat(user_input):
119
+ bot_response = chatbot_response(user_input)
120
+ return gr.Chatbot.update([[user_input, bot_response]])
121
+
122
+ submit_button.click(fn=handle_chat, inputs=question, outputs=chatbot)
123
+
124
+ if __name__ == "__main__":
125
+ demo.launch()