Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,18 @@ MODELS = [
|
|
| 12 |
"mistralai/Mistral-Nemo-Instruct-2407"
|
| 13 |
]
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
| 16 |
try:
|
| 17 |
results = list(DDGS().text(query, max_results=max_results))
|
|
@@ -22,16 +34,34 @@ def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str,
|
|
| 22 |
print(f"An error occurred during web search: {str(e)}")
|
| 23 |
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
| 24 |
|
| 25 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
context =
|
|
|
|
| 28 |
|
| 29 |
-
prompt = f"""Based on the following web search results about '{query}'
|
|
|
|
| 30 |
Include key facts, relevant statistics, and expert opinions if available.
|
| 31 |
Ensure the article is well-structured with an introduction, main body, and conclusion.
|
|
|
|
| 32 |
Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:
|
| 33 |
|
| 34 |
-
{
|
| 35 |
|
| 36 |
Article:"""
|
| 37 |
|
|
@@ -40,21 +70,30 @@ def summarize_results(query: str, search_results: List[Dict[str, str]], model: s
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"An error occurred during summarization: {str(e)}"
|
| 42 |
|
|
|
|
|
|
|
| 43 |
def respond(message, chat_history, model, temperature, num_api_calls):
|
| 44 |
final_summary = ""
|
|
|
|
|
|
|
| 45 |
for _ in range(num_api_calls):
|
| 46 |
-
search_results = get_web_search_results(
|
| 47 |
|
| 48 |
if not search_results:
|
| 49 |
-
final_summary += f"No search results found for the query: {
|
| 50 |
elif "error" in search_results[0]:
|
| 51 |
final_summary += search_results[0]["error"] + "\n\n"
|
| 52 |
else:
|
| 53 |
-
summary = summarize_results(
|
| 54 |
final_summary += summary + "\n\n"
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
| 58 |
css = """
|
| 59 |
Your custom CSS here
|
| 60 |
"""
|
|
@@ -107,4 +146,4 @@ demo = gr.ChatInterface(
|
|
| 107 |
)
|
| 108 |
)
|
| 109 |
|
| 110 |
-
demo.launch()
|
|
|
|
| 12 |
"mistralai/Mistral-Nemo-Instruct-2407"
|
| 13 |
]
|
| 14 |
|
| 15 |
+
class ConversationManager:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.history = []
|
| 18 |
+
self.current_context = None
|
| 19 |
+
|
| 20 |
+
def add_interaction(self, query, response):
|
| 21 |
+
self.history.append((query, response))
|
| 22 |
+
self.current_context = f"Previous query: {query}\nPrevious response summary: {response[:200]}..."
|
| 23 |
+
|
| 24 |
+
def get_context(self):
|
| 25 |
+
return self.current_context
|
| 26 |
+
|
| 27 |
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
| 28 |
try:
|
| 29 |
results = list(DDGS().text(query, max_results=max_results))
|
|
|
|
| 34 |
print(f"An error occurred during web search: {str(e)}")
|
| 35 |
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
| 36 |
|
| 37 |
+
def rephrase_query(original_query: str, conversation_manager: ConversationManager) -> str:
|
| 38 |
+
context = conversation_manager.get_context()
|
| 39 |
+
if context:
|
| 40 |
+
prompt = f"""Given the following context and a new query, rephrase the query to make it more specific and contextual:
|
| 41 |
+
|
| 42 |
+
Context: {context}
|
| 43 |
+
|
| 44 |
+
New query: {original_query}
|
| 45 |
+
|
| 46 |
+
Rephrased query:"""
|
| 47 |
+
|
| 48 |
+
rephrased_query = DDGS().chat(prompt, model="llama-3-70b")
|
| 49 |
+
return rephrased_query.strip()
|
| 50 |
+
return original_query
|
| 51 |
+
|
| 52 |
+
def summarize_results(query: str, search_results: List[Dict[str, str]], model: str, conversation_manager: ConversationManager) -> str:
|
| 53 |
try:
|
| 54 |
+
context = conversation_manager.get_context()
|
| 55 |
+
search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
|
| 56 |
|
| 57 |
+
prompt = f"""Based on the following web search results about '{query}' and considering the context: {context},
|
| 58 |
+
please create a comprehensive news article.
|
| 59 |
Include key facts, relevant statistics, and expert opinions if available.
|
| 60 |
Ensure the article is well-structured with an introduction, main body, and conclusion.
|
| 61 |
+
Address the query in the context of the ongoing conversation if applicable.
|
| 62 |
Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:
|
| 63 |
|
| 64 |
+
{search_context}
|
| 65 |
|
| 66 |
Article:"""
|
| 67 |
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
return f"An error occurred during summarization: {str(e)}"
|
| 72 |
|
| 73 |
+
conversation_manager = ConversationManager()
|
| 74 |
+
|
| 75 |
def respond(message, chat_history, model, temperature, num_api_calls):
|
| 76 |
final_summary = ""
|
| 77 |
+
rephrased_query = rephrase_query(message, conversation_manager)
|
| 78 |
+
|
| 79 |
for _ in range(num_api_calls):
|
| 80 |
+
search_results = get_web_search_results(rephrased_query)
|
| 81 |
|
| 82 |
if not search_results:
|
| 83 |
+
final_summary += f"No search results found for the query: {rephrased_query}\n\n"
|
| 84 |
elif "error" in search_results[0]:
|
| 85 |
final_summary += search_results[0]["error"] + "\n\n"
|
| 86 |
else:
|
| 87 |
+
summary = summarize_results(rephrased_query, search_results, model, conversation_manager)
|
| 88 |
final_summary += summary + "\n\n"
|
| 89 |
|
| 90 |
+
if final_summary:
|
| 91 |
+
conversation_manager.add_interaction(message, final_summary)
|
| 92 |
+
return final_summary
|
| 93 |
+
else:
|
| 94 |
+
return "Unable to generate a response. Please try a different query."
|
| 95 |
|
| 96 |
+
# The rest of your code (CSS, theme, and Gradio interface setup) remains the same
|
| 97 |
css = """
|
| 98 |
Your custom CSS here
|
| 99 |
"""
|
|
|
|
| 146 |
)
|
| 147 |
)
|
| 148 |
|
| 149 |
+
demo.launch()
|