Shreyas094
commited on
Commit
•
1a8ea50
1
Parent(s):
0c8a7e0
Update app.py
Browse files
app.py
CHANGED
@@ -33,6 +33,7 @@ import requests
|
|
33 |
from duckduckgo_search import DDGS
|
34 |
import random
|
35 |
import datetime
|
|
|
36 |
|
37 |
# Automatically get the current year
|
38 |
current_year = datetime.datetime.now().year
|
@@ -55,6 +56,12 @@ client = InferenceClient(
|
|
55 |
token=HF_TOKEN,
|
56 |
)
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Initialize the similarity model
|
59 |
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
|
60 |
|
@@ -495,7 +502,7 @@ def scrape_full_content(url, scraper="bs4", max_chars=3000, timeout=5):
|
|
495 |
logger.error(f"Error scraping full content from {url}: {e}")
|
496 |
return ""
|
497 |
|
498 |
-
def llm_summarize(json_input,
|
499 |
system_prompt = """You are Sentinel, a world-class Financial analysis AI model who is expert at searching the web and answering user's queries. You are also an expert at summarizing web pages or documents and searching for content in them."""
|
500 |
|
501 |
user_prompt = f"""
|
@@ -522,21 +529,32 @@ Your response should be detailed, informative, accurate, and directly relevant t
|
|
522 |
]
|
523 |
|
524 |
try:
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
533 |
except Exception as e:
|
534 |
logger.error(f"Error in LLM summarization: {e}")
|
535 |
return "Error: Unable to generate a summary. Please try again."
|
536 |
|
537 |
def search_and_scrape(query, chat_history, num_results=5, scraper="bs4", max_chars=3000, time_range="", language="all", category="",
|
538 |
-
engines=[], safesearch=2, method="GET", llm_temperature=0.2, timeout=5, use_duckduckgo=False):
|
539 |
-
|
540 |
try:
|
541 |
# Step 1: Rephrase the Query
|
542 |
rephrased_query = rephrase_query(chat_history, query, temperature=llm_temperature)
|
@@ -710,10 +728,10 @@ def search_and_scrape(query, chat_history, num_results=5, scraper="bs4", max_cha
|
|
710 |
} for doc in reranked_docs[:num_results]
|
711 |
]
|
712 |
}
|
713 |
-
|
714 |
# Step 6: LLM Summarization
|
715 |
-
llm_summary = llm_summarize(json.dumps(llm_input),
|
716 |
-
|
717 |
return llm_summary
|
718 |
|
719 |
except Exception as e:
|
@@ -721,7 +739,7 @@ def search_and_scrape(query, chat_history, num_results=5, scraper="bs4", max_cha
|
|
721 |
return f"An unexpected error occurred during the search and scrape process: {e}"
|
722 |
|
723 |
|
724 |
-
def chat_function(message, history, num_results, scraper, max_chars, time_range, language, category, engines, safesearch, method, llm_temperature, use_duckduckgo):
|
725 |
chat_history = "\n".join([f"{role}: {msg}" for role, msg in history])
|
726 |
|
727 |
response = search_and_scrape(
|
@@ -737,7 +755,8 @@ def chat_function(message, history, num_results, scraper, max_chars, time_range,
|
|
737 |
safesearch=safesearch,
|
738 |
method=method,
|
739 |
llm_temperature=llm_temperature,
|
740 |
-
use_duckduckgo=use_duckduckgo
|
|
|
741 |
)
|
742 |
|
743 |
yield response
|
@@ -764,6 +783,7 @@ iface = gr.ChatInterface(
|
|
764 |
gr.Radio(["GET", "POST"], value="POST", label="HTTP Method"),
|
765 |
gr.Slider(0, 1, value=0.2, step=0.1, label="LLM Temperature"),
|
766 |
gr.Checkbox(label="Use DuckDuckGo Search", value=False),
|
|
|
767 |
],
|
768 |
additional_inputs_accordion=gr.Accordion("⚙️ Advanced Parameters", open=True),
|
769 |
retry_btn="Retry",
|
|
|
33 |
from duckduckgo_search import DDGS
|
34 |
import random
|
35 |
import datetime
|
36 |
+
from groq import Groq
|
37 |
|
38 |
# Automatically get the current year
|
39 |
current_year = datetime.datetime.now().year
|
|
|
56 |
token=HF_TOKEN,
|
57 |
)
|
58 |
|
59 |
+
# Default API key for examples (replace with a dummy value or leave empty)
|
60 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
61 |
+
|
62 |
+
# Initialize Groq client
|
63 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
64 |
+
|
65 |
# Initialize the similarity model
|
66 |
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
|
67 |
|
|
|
502 |
logger.error(f"Error scraping full content from {url}: {e}")
|
503 |
return ""
|
504 |
|
505 |
+
def llm_summarize(json_input, model, temperature=0.2):
|
506 |
system_prompt = """You are Sentinel, a world-class Financial analysis AI model who is expert at searching the web and answering user's queries. You are also an expert at summarizing web pages or documents and searching for content in them."""
|
507 |
|
508 |
user_prompt = f"""
|
|
|
529 |
]
|
530 |
|
531 |
try:
|
532 |
+
if model == "groq":
|
533 |
+
response = groq_client.chat.completions.create(
|
534 |
+
messages=messages,
|
535 |
+
model="llama-3.1-8b-instruct",
|
536 |
+
max_tokens=8192,
|
537 |
+
temperature=temperature,
|
538 |
+
top_p=0.9,
|
539 |
+
frequency_penalty=1.2,
|
540 |
+
stream=False
|
541 |
+
)
|
542 |
+
return response.choices[0].message.content.strip()
|
543 |
+
else:
|
544 |
+
response = client.chat_completion(
|
545 |
+
messages=messages,
|
546 |
+
max_tokens=10000,
|
547 |
+
temperature=temperature,
|
548 |
+
frequency_penalty=1.4,
|
549 |
+
top_p=0.9
|
550 |
+
)
|
551 |
+
return response.choices[0].message.content.strip()
|
552 |
except Exception as e:
|
553 |
logger.error(f"Error in LLM summarization: {e}")
|
554 |
return "Error: Unable to generate a summary. Please try again."
|
555 |
|
556 |
def search_and_scrape(query, chat_history, num_results=5, scraper="bs4", max_chars=3000, time_range="", language="all", category="",
|
557 |
+
engines=[], safesearch=2, method="GET", llm_temperature=0.2, timeout=5, use_duckduckgo=False, model="huggingface"):
|
|
|
558 |
try:
|
559 |
# Step 1: Rephrase the Query
|
560 |
rephrased_query = rephrase_query(chat_history, query, temperature=llm_temperature)
|
|
|
728 |
} for doc in reranked_docs[:num_results]
|
729 |
]
|
730 |
}
|
731 |
+
|
732 |
# Step 6: LLM Summarization
|
733 |
+
llm_summary = llm_summarize(json.dumps(llm_input), model, temperature=llm_temperature)
|
734 |
+
|
735 |
return llm_summary
|
736 |
|
737 |
except Exception as e:
|
|
|
739 |
return f"An unexpected error occurred during the search and scrape process: {e}"
|
740 |
|
741 |
|
742 |
+
def chat_function(message, history, num_results, scraper, max_chars, time_range, language, category, engines, safesearch, method, llm_temperature, use_duckduckgo, model):
|
743 |
chat_history = "\n".join([f"{role}: {msg}" for role, msg in history])
|
744 |
|
745 |
response = search_and_scrape(
|
|
|
755 |
safesearch=safesearch,
|
756 |
method=method,
|
757 |
llm_temperature=llm_temperature,
|
758 |
+
use_duckduckgo=use_duckduckgo,
|
759 |
+
model=model
|
760 |
)
|
761 |
|
762 |
yield response
|
|
|
783 |
gr.Radio(["GET", "POST"], value="POST", label="HTTP Method"),
|
784 |
gr.Slider(0, 1, value=0.2, step=0.1, label="LLM Temperature"),
|
785 |
gr.Checkbox(label="Use DuckDuckGo Search", value=False),
|
786 |
+
gr.Dropdown(["huggingface", "groq"], value="huggingface", label="LLM Model"),
|
787 |
],
|
788 |
additional_inputs_accordion=gr.Accordion("⚙️ Advanced Parameters", open=True),
|
789 |
retry_btn="Retry",
|