Shreyas094 commited on
Commit
96b8cb4
·
verified ·
1 Parent(s): 2bcd6e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -9
app.py CHANGED
@@ -6,12 +6,33 @@ import urllib.parse
6
  import random
7
  import os
8
  from dotenv import load_dotenv
 
 
9
 
10
  load_dotenv() # Load environment variables from .env file
11
 
12
  # Now replace the hard-coded token with the environment variable
13
  HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  _useragent_list = [
16
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
17
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
@@ -254,17 +275,20 @@ def scrape_and_display(query, num_results, instructions, web_search=True, temper
254
  return generated_summary
255
 
256
  # Main Gradio interface function
257
- def gradio_interface(query, use_pdf, pdf, num_results, instructions, temperature, repetition_penalty, top_p):
 
 
 
258
  if use_pdf and pdf is not None:
259
  pdf_text = read_pdf(pdf)
260
  generated_summary = scrape_and_display(pdf_text, num_results=0, instructions=instructions, web_search=False, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
261
  else:
262
  generated_summary = scrape_and_display(query, num_results=num_results, instructions=instructions, web_search=True, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
263
-
264
  # Save the generated summary to a PDF
265
  output_pdf_path = "output_summary.pdf"
266
  save_text_to_pdf(generated_summary, output_pdf_path)
267
-
268
  return generated_summary, output_pdf_path
269
 
270
  # Deploy Gradio Interface
@@ -274,13 +298,15 @@ gr.Interface(
274
  gr.Textbox(label="Query"),
275
  gr.Checkbox(label="Use PDF"),
276
  gr.File(label="Upload PDF"),
277
- gr.Slider(minimum=1, maximum=20, label="Number of Results"), # Added Slider for num_results
278
  gr.Textbox(label="Instructions"),
279
- gr.Slider(minimum=0.1, maximum=1.0, label="Temperature"),
280
- gr.Slider(minimum=0.1, maximum=1.0, label="Repetition Penalty"),
281
- gr.Slider(minimum=0.1, maximum=1.0, label="Top p")
 
282
  ],
283
- outputs=["text", "file"], # Updated to return text and a file
284
  title="Financial Analyst AI Assistant",
285
- description="Enter your query about a company's financials to get valuable insights. Optionally, upload a PDF for analysis.Please instruct me for curating your output template, also for web search you can modify my search results but its advisable to restrict the same at 10. You can also adjust my parameters like Temperature, Repetition Penalty and Top_P, its adivsable to set repetition penalty at 1 and other two parameters at 0.1.",
 
286
  ).launch(share=True)
 
6
  import random
7
  import os
8
  from dotenv import load_dotenv
9
+ import shutil
10
+ import tempfile
11
 
12
  load_dotenv() # Load environment variables from .env file
13
 
14
  # Now replace the hard-coded token with the environment variable
15
  HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
16
 
17
+ def clear_cache():
18
+ try:
19
+ # Clear Gradio cache
20
+ cache_dir = tempfile.gettempdir()
21
+ shutil.rmtree(os.path.join(cache_dir, "gradio"), ignore_errors=True)
22
+
23
+ # Clear any custom cache you might have
24
+ # For example, if you're caching PDF files or search results:
25
+ if os.path.exists("output_summary.pdf"):
26
+ os.remove("output_summary.pdf")
27
+
28
+ # Add any other cache clearing operations here
29
+
30
+ print("Cache cleared successfully.")
31
+ return "Cache cleared successfully."
32
+ except Exception as e:
33
+ print(f"Error clearing cache: {e}")
34
+ return f"Error clearing cache: {e}"
35
+
36
  _useragent_list = [
37
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
38
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
 
275
  return generated_summary
276
 
277
  # Main Gradio interface function
278
+ def gradio_interface(query, use_pdf, pdf, num_results, instructions, temperature, repetition_penalty, top_p, clear_cache_flag):
279
+ if clear_cache_flag:
280
+ return clear_cache()
281
+
282
  if use_pdf and pdf is not None:
283
  pdf_text = read_pdf(pdf)
284
  generated_summary = scrape_and_display(pdf_text, num_results=0, instructions=instructions, web_search=False, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
285
  else:
286
  generated_summary = scrape_and_display(query, num_results=num_results, instructions=instructions, web_search=True, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
287
+
288
  # Save the generated summary to a PDF
289
  output_pdf_path = "output_summary.pdf"
290
  save_text_to_pdf(generated_summary, output_pdf_path)
291
+
292
  return generated_summary, output_pdf_path
293
 
294
  # Deploy Gradio Interface
 
298
  gr.Textbox(label="Query"),
299
  gr.Checkbox(label="Use PDF"),
300
  gr.File(label="Upload PDF"),
301
+ gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Number of Results"),
302
  gr.Textbox(label="Instructions"),
303
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
304
+ gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Repetition Penalty"),
305
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top p"),
306
+ gr.Checkbox(label="Clear Cache", visible=False) # Hidden checkbox for clear cache functionality
307
  ],
308
+ outputs=["text", gr.File(label="Generated PDF")],
309
  title="Financial Analyst AI Assistant",
310
+ description="Enter your query about a company's financials to get valuable insights. Optionally, upload a PDF for analysis. Please instruct me for curating your output template. For web search, you can modify my search results but it's advisable to restrict them to 10. You can also adjust parameters like Temperature, Repetition Penalty, and Top_P. It's advisable to set repetition penalty at 1 and other two parameters at 0.1.",
311
+ allow_flagging="never"
312
  ).launch(share=True)