Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,13 @@ import inspect
|
|
19 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
20 |
llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Initialize LlamaParse
|
23 |
llama_parser = LlamaParse(
|
24 |
api_key=llama_cloud_api_key,
|
@@ -71,9 +78,9 @@ def update_vectors(files, parser):
|
|
71 |
|
72 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
|
73 |
|
74 |
-
def generate_chunked_response(prompt, max_tokens=1000, max_chunks=5, temperature=0.7):
|
75 |
client = InferenceClient(
|
76 |
-
|
77 |
token=huggingface_token,
|
78 |
)
|
79 |
|
@@ -122,7 +129,7 @@ class CitingSources(BaseModel):
|
|
122 |
description="List of sources to cite. Should be an URL of the source."
|
123 |
)
|
124 |
|
125 |
-
def get_response_from_pdf(query, temperature=0.7):
|
126 |
embed = get_embeddings()
|
127 |
if os.path.exists("faiss_database"):
|
128 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
@@ -146,7 +153,7 @@ Do not include a list of sources in your response. [/INST]"""
|
|
146 |
|
147 |
return clean_text
|
148 |
|
149 |
-
def get_response_with_search(query, temperature=0.7):
|
150 |
search_results = duckduckgo_search(query)
|
151 |
context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
|
152 |
for result in search_results if 'body' in result)
|
@@ -169,12 +176,12 @@ After writing the document, please provide a list of sources used in your respon
|
|
169 |
|
170 |
return main_content, sources
|
171 |
|
172 |
-
def chatbot_interface(message, history, use_web_search, temperature):
|
173 |
if use_web_search:
|
174 |
-
main_content, sources = get_response_with_search(message, temperature)
|
175 |
formatted_response = f"{main_content}\n\nSources:\n{sources}"
|
176 |
else:
|
177 |
-
response = get_response_from_pdf(message, temperature)
|
178 |
formatted_response = response
|
179 |
|
180 |
history.append((message, formatted_response))
|
@@ -182,7 +189,7 @@ def chatbot_interface(message, history, use_web_search, temperature):
|
|
182 |
|
183 |
# Gradio interface
|
184 |
with gr.Blocks() as demo:
|
185 |
-
gr.Markdown("# AI-powered Web Search and PDF Chat Assistant
|
186 |
|
187 |
with gr.Row():
|
188 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
@@ -197,7 +204,8 @@ with gr.Blocks() as demo:
|
|
197 |
use_web_search = gr.Checkbox(label="Use Web Search", value=False)
|
198 |
|
199 |
with gr.Row():
|
200 |
-
|
|
|
201 |
|
202 |
submit = gr.Button("Submit")
|
203 |
|
@@ -212,10 +220,10 @@ with gr.Blocks() as demo:
|
|
212 |
)
|
213 |
|
214 |
submit.click(chatbot_interface,
|
215 |
-
inputs=[msg, chatbot, use_web_search, temperature_slider],
|
216 |
outputs=[chatbot])
|
217 |
msg.submit(chatbot_interface,
|
218 |
-
inputs=[msg, chatbot, use_web_search, temperature_slider],
|
219 |
outputs=[chatbot])
|
220 |
|
221 |
gr.Markdown(
|
|
|
19 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
20 |
llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
21 |
|
22 |
+
MODELS = [
|
23 |
+
"meta-llama/Meta-Llama-3.1-70B-Instruct",
|
24 |
+
"meta-llama/Meta-Llama-3-70B-Instruct",
|
25 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
26 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
27 |
+
]
|
28 |
+
|
29 |
# Initialize LlamaParse
|
30 |
llama_parser = LlamaParse(
|
31 |
api_key=llama_cloud_api_key,
|
|
|
78 |
|
79 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
|
80 |
|
81 |
+
def generate_chunked_response(prompt, model, max_tokens=1000, max_chunks=5, temperature=0.7):
|
82 |
client = InferenceClient(
|
83 |
+
model,
|
84 |
token=huggingface_token,
|
85 |
)
|
86 |
|
|
|
129 |
description="List of sources to cite. Should be an URL of the source."
|
130 |
)
|
131 |
|
132 |
+
def get_response_from_pdf(query, model, temperature=0.7):
|
133 |
embed = get_embeddings()
|
134 |
if os.path.exists("faiss_database"):
|
135 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
|
|
153 |
|
154 |
return clean_text
|
155 |
|
156 |
+
def get_response_with_search(query, model, temperature=0.7):
|
157 |
search_results = duckduckgo_search(query)
|
158 |
context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
|
159 |
for result in search_results if 'body' in result)
|
|
|
176 |
|
177 |
return main_content, sources
|
178 |
|
179 |
+
def chatbot_interface(message, history, use_web_search, model, temperature):
|
180 |
if use_web_search:
|
181 |
+
main_content, sources = get_response_with_search(message, model, temperature)
|
182 |
formatted_response = f"{main_content}\n\nSources:\n{sources}"
|
183 |
else:
|
184 |
+
response = get_response_from_pdf(message, model, temperature)
|
185 |
formatted_response = response
|
186 |
|
187 |
history.append((message, formatted_response))
|
|
|
189 |
|
190 |
# Gradio interface
|
191 |
with gr.Blocks() as demo:
|
192 |
+
gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
|
193 |
|
194 |
with gr.Row():
|
195 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
|
|
204 |
use_web_search = gr.Checkbox(label="Use Web Search", value=False)
|
205 |
|
206 |
with gr.Row():
|
207 |
+
model_dropdown = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2])
|
208 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
|
209 |
|
210 |
submit = gr.Button("Submit")
|
211 |
|
|
|
220 |
)
|
221 |
|
222 |
submit.click(chatbot_interface,
|
223 |
+
inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider],
|
224 |
outputs=[chatbot])
|
225 |
msg.submit(chatbot_interface,
|
226 |
+
inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider],
|
227 |
outputs=[chatbot])
|
228 |
|
229 |
gr.Markdown(
|