Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
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 |
-
max_tokens,
|
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 |
if __name__ == "__main__":
|
104 |
-
|
|
|
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()
|