Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,9 +5,6 @@ from typing import List, Dict, Any
|
|
| 5 |
from helper_functions_api import md_to_html, search_brave, fetch_and_extract_content, limit_tokens, together_response, insert_data
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv, find_dotenv
|
| 8 |
-
from fastapi_cache import FastAPICache
|
| 9 |
-
from fastapi_cache.decorator import cache
|
| 10 |
-
from fastapi_cache.backends.inmemory import InMemoryBackend
|
| 11 |
|
| 12 |
# Load environment variables from .env file
|
| 13 |
#load_dotenv("keys.env")
|
|
@@ -57,12 +54,7 @@ class QueryModel(BaseModel):
|
|
| 57 |
output_format: str = Query(default="Tabular Report", description="Output format for the report", enum=["Chat", "Full Text Report", "Tabular Report", "Tables only"])
|
| 58 |
data_format: str = Query(default="Structured data", description="Type of data to extract from the internet", enum=["No presets", "Structured data", "Quantitative data"])
|
| 59 |
|
| 60 |
-
@app.on_event("startup")
|
| 61 |
-
async def startup():
|
| 62 |
-
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
|
| 63 |
-
|
| 64 |
@app.post("/generate_report")
|
| 65 |
-
#@cache(expire=60 * 60 * 24 * 7) # Cache for 7 days
|
| 66 |
async def generate_report(request: Request, query: QueryModel):
|
| 67 |
query_str = query.topic
|
| 68 |
description = query.description
|
|
@@ -75,16 +67,12 @@ async def generate_report(request: Request, query: QueryModel):
|
|
| 75 |
|
| 76 |
# Combine query with user keywords
|
| 77 |
if query.internet:
|
| 78 |
-
search_query = description
|
| 79 |
# Search for relevant URLs
|
| 80 |
try:
|
| 81 |
-
urls,optimized_search_query = search_brave(search_query, num_results=
|
| 82 |
# Fetch and extract content from the URLs
|
| 83 |
-
all_text_with_urls = fetch_and_extract_content(
|
| 84 |
-
data_format=data_format,
|
| 85 |
-
urls=urls,
|
| 86 |
-
query=query,
|
| 87 |
-
num_refrences=8)
|
| 88 |
# Prepare the prompt for generating the report
|
| 89 |
additional_context = limit_tokens(str(all_text_with_urls))
|
| 90 |
prompt = f"#### COMPLETE THE TASK: {description} #### IN THE CONTEXT OF ### CONTEXT: {query_str} USING THE #### SCRAPED DATA:{additional_context}"
|
|
@@ -98,7 +86,6 @@ async def generate_report(request: Request, query: QueryModel):
|
|
| 98 |
|
| 99 |
md_report = together_response(prompt, model=llm_default_medium, SysPrompt=sys_prompt_output_format)
|
| 100 |
|
| 101 |
-
# Insert data into database (or other storage)
|
| 102 |
if user_id != "test":
|
| 103 |
insert_data(user_id, query_str, description, str(all_text_with_urls), md_report)
|
| 104 |
references_html = dict()
|
|
|
|
| 5 |
from helper_functions_api import md_to_html, search_brave, fetch_and_extract_content, limit_tokens, together_response, insert_data
|
| 6 |
import os
|
| 7 |
from dotenv import load_dotenv, find_dotenv
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load environment variables from .env file
|
| 10 |
#load_dotenv("keys.env")
|
|
|
|
| 54 |
output_format: str = Query(default="Tabular Report", description="Output format for the report", enum=["Chat", "Full Text Report", "Tabular Report", "Tables only"])
|
| 55 |
data_format: str = Query(default="Structured data", description="Type of data to extract from the internet", enum=["No presets", "Structured data", "Quantitative data"])
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.post("/generate_report")
|
|
|
|
| 58 |
async def generate_report(request: Request, query: QueryModel):
|
| 59 |
query_str = query.topic
|
| 60 |
description = query.description
|
|
|
|
| 67 |
|
| 68 |
# Combine query with user keywords
|
| 69 |
if query.internet:
|
| 70 |
+
search_query = description
|
| 71 |
# Search for relevant URLs
|
| 72 |
try:
|
| 73 |
+
urls,optimized_search_query = search_brave(search_query, num_results=4)
|
| 74 |
# Fetch and extract content from the URLs
|
| 75 |
+
all_text_with_urls = fetch_and_extract_content(data_format, urls, query_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Prepare the prompt for generating the report
|
| 77 |
additional_context = limit_tokens(str(all_text_with_urls))
|
| 78 |
prompt = f"#### COMPLETE THE TASK: {description} #### IN THE CONTEXT OF ### CONTEXT: {query_str} USING THE #### SCRAPED DATA:{additional_context}"
|
|
|
|
| 86 |
|
| 87 |
md_report = together_response(prompt, model=llm_default_medium, SysPrompt=sys_prompt_output_format)
|
| 88 |
|
|
|
|
| 89 |
if user_id != "test":
|
| 90 |
insert_data(user_id, query_str, description, str(all_text_with_urls), md_report)
|
| 91 |
references_html = dict()
|