Shreyas094 commited on
Commit
0a74a16
·
verified ·
1 Parent(s): 96b8cb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -14
app.py CHANGED
@@ -33,6 +33,24 @@ def clear_cache():
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",
@@ -131,24 +149,33 @@ def format_prompt(query, search_results, instructions):
131
  else:
132
  formatted_results += "No link found.\n" + '-' * 80 + '\n'
133
 
134
- prompt = f"{instructions}User Query: {query}\n\nWeb Search Results:\n{formatted_results}\n\nAssistant:"
 
 
 
 
 
 
 
 
135
  return prompt
136
 
137
  # Function to generate text using Hugging Face API
138
- def generate_text(input_text, temperature=0.7, repetition_penalty=1.0, top_p=0.9):
139
  print("Generating text using Hugging Face API...")
140
  endpoint = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
141
  headers = {
142
- "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}", # Use the environment variable
143
  "Content-Type": "application/json"
144
  }
145
  data = {
146
  "inputs": input_text,
147
  "parameters": {
148
- "max_new_tokens": 8000, # Adjust as needed
149
  "temperature": temperature,
150
  "repetition_penalty": repetition_penalty,
151
- "top_p": top_p
 
152
  }
153
  }
154
 
@@ -275,17 +302,24 @@ def scrape_and_display(query, num_results, instructions, web_search=True, temper
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
 
@@ -295,18 +329,19 @@ def gradio_interface(query, use_pdf, pdf, num_results, instructions, temperature
295
  gr.Interface(
296
  fn=gradio_interface,
297
  inputs=[
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)
 
33
  print(f"Error clearing cache: {e}")
34
  return f"Error clearing cache: {e}"
35
 
36
+ PREDEFINED_QUERIES = {
37
+ "Recent Earnings": {
38
+ "query": "{company} recent quarterly earnings",
39
+ "instructions": "Provide the most recent quarterly earnings data for {company}. Include revenue, net income, and EPS. Specify the exact quarter and year."
40
+ },
41
+ "Recent News": {
42
+ "query": "{company} recent news",
43
+ "instructions": "Summarize the most recent significant news about {company}. Focus on events that could impact the company's financial performance or stock price."
44
+ },
45
+ "Credit Rating": {
46
+ "query": "{company} current credit rating",
47
+ "instructions": "Provide the most recent credit rating for {company}. Include the rating agency, the exact rating, and the date it was issued or last confirmed."
48
+ },
49
+ "Earnings Call Transcript": {
50
+ "query": "{company} most recent earnings call transcript",
51
+ "instructions": "Summarize key points from {company}'s most recent earnings call. Include date of the call, major financial highlights, and any significant forward-looking statements."
52
+ }
53
+ }
54
  _useragent_list = [
55
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
56
  "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",
 
149
  else:
150
  formatted_results += "No link found.\n" + '-' * 80 + '\n'
151
 
152
+ prompt = f"""Instructions: {instructions}
153
+ User Query: {query}
154
+
155
+ Web Search Results:
156
+ {formatted_results}
157
+
158
+ Important: Provide a precise and factual response based solely on the information given above. Include specific dates, numbers, and sources where available. If exact information is not provided in the search results, clearly state that the information is not available in the given context. Do not make assumptions or provide information that is not directly supported by the search results.
159
+
160
+ Assistant:"""
161
  return prompt
162
 
163
  # Function to generate text using Hugging Face API
164
+ def generate_text(input_text, temperature=0.3, repetition_penalty=1.2, top_p=0.9):
165
  print("Generating text using Hugging Face API...")
166
  endpoint = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
167
  headers = {
168
+ "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
169
  "Content-Type": "application/json"
170
  }
171
  data = {
172
  "inputs": input_text,
173
  "parameters": {
174
+ "max_new_tokens": 1000, # Reduced to focus on more concise answers
175
  "temperature": temperature,
176
  "repetition_penalty": repetition_penalty,
177
+ "top_p": top_p,
178
+ "do_sample": True
179
  }
180
  }
181
 
 
302
  return generated_summary
303
 
304
  # Main Gradio interface function
305
+ def gradio_interface(query, use_dashboard, use_pdf, pdf, num_results, custom_instructions, temperature, repetition_penalty, top_p, clear_cache_flag):
306
  if clear_cache_flag:
307
  return clear_cache()
308
 
309
+ if use_dashboard:
310
+ results = []
311
+ for query_type, query_info in PREDEFINED_QUERIES.items():
312
+ formatted_query = query_info['query'].format(company=query)
313
+ formatted_instructions = query_info['instructions'].format(company=query)
314
+ result = scrape_and_display(formatted_query, num_results=num_results, instructions=formatted_instructions, web_search=True, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
315
+ results.append(f"**{query_type}**\n\n{result}\n\n")
316
+ generated_summary = "\n".join(results)
317
+ elif use_pdf and pdf is not None:
318
  pdf_text = read_pdf(pdf)
319
+ generated_summary = scrape_and_display(pdf_text, num_results=0, instructions=custom_instructions, web_search=False, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
320
  else:
321
+ generated_summary = scrape_and_display(query, num_results=num_results, instructions=custom_instructions, web_search=True, temperature=temperature, repetition_penalty=repetition_penalty, top_p=top_p)
322
 
 
323
  output_pdf_path = "output_summary.pdf"
324
  save_text_to_pdf(generated_summary, output_pdf_path)
325
 
 
329
  gr.Interface(
330
  fn=gradio_interface,
331
  inputs=[
332
+ gr.Textbox(label="Company Name or Query"),
333
+ gr.Checkbox(label="Use Dashboard"),
334
  gr.Checkbox(label="Use PDF"),
335
  gr.File(label="Upload PDF"),
336
  gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Number of Results"),
337
+ gr.Textbox(label="Custom Instructions"),
338
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
339
  gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Repetition Penalty"),
340
  gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top p"),
341
+ gr.Checkbox(label="Clear Cache", visible=False)
342
  ],
343
  outputs=["text", gr.File(label="Generated PDF")],
344
  title="Financial Analyst AI Assistant",
345
+ description="Enter a company name to get a financial dashboard, or enter a custom query. Optionally, upload a PDF for analysis. Adjust parameters as needed for optimal results.",
346
  allow_flagging="never"
347
  ).launch(share=True)