AI-RESEARCHER-2024 commited on
Commit
c61718b
·
verified ·
1 Parent(s): abdef6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -95
app.py CHANGED
@@ -5,100 +5,197 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_community.vectorstores import Chroma
6
  from langchain.prompts import PromptTemplate
7
 
8
- # Initialize the embedding model
9
- embeddings = HuggingFaceEmbeddings(
10
- model_name="sentence-transformers/all-MiniLM-L6-v2",
11
- model_kwargs={'device': 'cpu'},
12
- encode_kwargs={'normalize_embeddings': True}
13
- )
14
-
15
- # Load the existing Chroma vector store
16
- persist_directory = os.path.join(os.path.dirname(__file__), 'mydb')
17
- vectorstore = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
18
-
19
- # Initialize the Llama model
20
- llm = Llama.from_pretrained(
21
- repo_id="bartowski/Llama-3.2-1B-Instruct-GGUF",
22
- filename="Llama-3.2-1B-Instruct-Q8_0.gguf",
23
- )
24
-
25
- # Create the RAG prompt template
26
- template = """Answer the question based only on the following context:
27
-
28
- {context}
29
-
30
- Question: {question}
31
-
32
- Answer the question in a clear way. If you cannot find the answer in the context, just say "I don't have enough information to answer this question."
33
-
34
- Make sure to:
35
- 1. Only use information from the provided context
36
- 2. If you're unsure, acknowledge it
37
- """
38
-
39
- prompt = PromptTemplate.from_template(template)
40
-
41
- def respond(
42
- message,
43
- history,
44
- system_message,
45
- max_tokens,
46
- temperature,
47
- # top_p,
48
- ):
49
- # Build the messages list
50
- messages = [{"role": "system", "content": system_message}]
51
-
52
- for user_msg, assistant_msg in history:
53
- if user_msg:
54
- messages.append({"role": "user", "content": user_msg})
55
- if assistant_msg:
56
- messages.append({"role": "assistant", "content": assistant_msg})
57
-
58
- # Search the vector store
59
- retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
60
- docs = retriever.get_relevant_documents(message)
61
- context = "\n\n".join([doc.page_content for doc in docs])
62
-
63
- # Format the prompt
64
- final_prompt = prompt.format(context=context, question=message)
65
-
66
- # Add the formatted prompt to messages
67
- messages.append({"role": "user", "content": final_prompt})
68
-
69
- # Generate response using the Llama model
70
- response = llm.create_chat_completion(
71
- messages=messages,
72
- max_tokens=max_tokens,
73
- temperature=temperature,
74
- # top_p=top_p,
75
- )
76
-
77
- # Extract the assistant's reply
78
- assistant_reply = response['choices'][0]['message']['content']
79
-
80
- return assistant_reply
81
-
82
- # Create Gradio Chat Interface
83
- demo = gr.ChatInterface(
84
- fn=respond,
85
- additional_inputs=[
86
- gr.Textbox(value="You are a friendly chatbot.", label="System Message"),
87
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
88
- gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
89
- # gr.Slider(
90
- # minimum=0.1,
91
- # maximum=1.0,
92
- # value=0.95,
93
- # step=0.05,
94
- # label="Top-p (Nucleus Sampling)",
95
- # ),
96
- ],
97
- title="Document-Based QA with Llama",
98
- description="A PDF Chat interface powered by the Llama model.",
99
- examples=["What is a Computer?"],
100
- theme="default",
101
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  if __name__ == "__main__":
104
- demo.launch()
 
5
  from langchain_community.vectorstores import Chroma
6
  from langchain.prompts import PromptTemplate
7
 
8
+ class RAGInterface:
9
+ def __init__(self):
10
+ # Initialize embedding model
11
+ self.embeddings = HuggingFaceEmbeddings(
12
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
13
+ model_kwargs={'device': 'cpu'},
14
+ encode_kwargs={'normalize_embeddings': True}
15
+ )
16
+
17
+ # Load vector store
18
+ persist_directory = os.path.join(os.path.dirname(__file__), 'mydb')
19
+ self.vectorstore = Chroma(
20
+ persist_directory=persist_directory,
21
+ embedding_function=self.embeddings
22
+ )
23
+
24
+ # Initialize LLM
25
+ self.llm = Llama.from_pretrained(
26
+ repo_id="bartowski/Llama-3.2-1B-Instruct-GGUF",
27
+ filename="Llama-3.2-1B-Instruct-Q8_0.gguf",
28
+ )
29
+
30
+ # Define RAG prompt template
31
+ self.template = """Answer the question based only on the following context:
32
+ {context}
33
+
34
+ Question: {question}
35
+
36
+ Answer the question in a clear way. If you cannot find the answer in the context,
37
+ just say "I don't have enough information to answer this question."
38
+
39
+ Make sure to:
40
+ 1. Only use information from the provided context
41
+ 2. If you're unsure, acknowledge it
42
+ """
43
+ self.prompt = PromptTemplate.from_template(self.template)
44
+
45
+ def respond(self, message, history, system_message, max_tokens, temperature):
46
+ # Build messages list
47
+ messages = [{"role": "system", "content": system_message}]
48
+ for user_msg, assistant_msg in history:
49
+ if user_msg:
50
+ messages.append({"role": "user", "content": user_msg})
51
+ if assistant_msg:
52
+ messages.append({"role": "assistant", "content": assistant_msg})
53
+
54
+ # Search vector store
55
+ retriever = self.vectorstore.as_retriever(search_kwargs={"k": 5})
56
+ docs = retriever.get_relevant_documents(message)
57
+ context = "\n\n".join([doc.page_content for doc in docs])
58
+
59
+ # Format prompt and add to messages
60
+ final_prompt = self.prompt.format(context=context, question=message)
61
+ messages.append({"role": "user", "content": final_prompt})
62
+
63
+ # Generate response
64
+ response = self.llm.create_chat_completion(
65
+ messages=messages,
66
+ max_tokens=max_tokens,
67
+ temperature=temperature,
68
+ )
69
+
70
+ return response['choices'][0]['message']['content']
71
+
72
+ def create_interface(self):
73
+ # Custom CSS for better styling
74
+ custom_css = """
75
+ <style>
76
+ /* Global Styles */
77
+ body, #root {
78
+ font-family: Helvetica, Arial, sans-serif;
79
+ background-color: #1a1a1a;
80
+ color: #fafafa;
81
+ }
82
+
83
+ /* Header Styles */
84
+ .app-header {
85
+ background: linear-gradient(45deg, #1a1a1a 0%, #333333 100%);
86
+ padding: 24px;
87
+ border-radius: 8px;
88
+ margin-bottom: 24px;
89
+ text-align: center;
90
+ }
91
+
92
+ .app-title {
93
+ font-size: 36px;
94
+ margin: 0;
95
+ color: #fafafa;
96
+ }
97
+
98
+ .app-subtitle {
99
+ font-size: 18px;
100
+ margin: 8px 0;
101
+ color: #fafafa;
102
+ opacity: 0.8;
103
+ }
104
+
105
+ /* Chat Container */
106
+ .chat-container {
107
+ background-color: #2a2a2a;
108
+ border-radius: 8px;
109
+ padding: 20px;
110
+ margin-bottom: 20px;
111
+ }
112
+
113
+ /* Control Panel */
114
+ .control-panel {
115
+ background-color: #333;
116
+ padding: 16px;
117
+ border-radius: 8px;
118
+ margin-top: 16px;
119
+ }
120
+
121
+ /* Gradio Component Overrides */
122
+ .gr-button {
123
+ background-color: #4a4a4a;
124
+ color: #fff;
125
+ border: none;
126
+ border-radius: 4px;
127
+ padding: 8px 16px;
128
+ transition: background-color 0.3s;
129
+ }
130
+
131
+ .gr-button:hover {
132
+ background-color: #5a5a5a;
133
+ }
134
+
135
+ .gr-input, .gr-dropdown {
136
+ background-color: #3a3a3a;
137
+ color: #fff;
138
+ border: 1px solid #4a4a4a;
139
+ border-radius: 4px;
140
+ padding: 8px;
141
+ }
142
+ </style>
143
+ """
144
+
145
+ # Header HTML
146
+ header_html = f"""
147
+ <div class="app-header">
148
+ <h1 class="app-title">Document-Based Question Answering</h1>
149
+ <h2 class="app-subtitle">Powered by Llama and RAG</h2>
150
+ </div>
151
+ {custom_css}
152
+ """
153
+
154
+ # Create Gradio interface
155
+ demo = gr.ChatInterface(
156
+ fn=self.respond,
157
+ additional_inputs=[
158
+ gr.Textbox(
159
+ value="You are a friendly chatbot.",
160
+ label="System Message",
161
+ elem_classes="control-panel"
162
+ ),
163
+ gr.Slider(
164
+ minimum=1,
165
+ maximum=2048,
166
+ value=512,
167
+ step=1,
168
+ label="Max New Tokens",
169
+ elem_classes="control-panel"
170
+ ),
171
+ gr.Slider(
172
+ minimum=0.1,
173
+ maximum=1.0,
174
+ value=0.7,
175
+ step=0.1,
176
+ label="Temperature",
177
+ elem_classes="control-panel"
178
+ ),
179
+ ],
180
+ title="", # Title is handled in custom HTML
181
+ description="Ask questions about your documents and get AI-powered answers.",
182
+ examples=[
183
+ "What is a Computer?",
184
+ "How does machine learning work?",
185
+ "Explain artificial intelligence.",
186
+ ],
187
+ theme=gr.themes.Default(),
188
+ )
189
+
190
+ # Wrap the interface with custom HTML
191
+ return gr.Blocks(css=custom_css) as wrapped_demo:
192
+ gr.HTML(header_html)
193
+ demo.render()
194
+
195
+ def main():
196
+ interface = RAGInterface()
197
+ demo = interface.create_interface()
198
+ demo.launch(debug=True)
199
 
200
  if __name__ == "__main__":
201
+ main()