Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,13 @@ import os
|
|
| 7 |
# Environment variables and configurations
|
| 8 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
| 11 |
try:
|
| 12 |
results = DDGS().text(query, max_results=max_results)
|
|
@@ -14,7 +21,7 @@ def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str,
|
|
| 14 |
except Exception as e:
|
| 15 |
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
| 16 |
|
| 17 |
-
def summarize_results(query: str, search_results: List[Dict[str, str]]) -> str:
|
| 18 |
try:
|
| 19 |
context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
|
| 20 |
|
|
@@ -27,7 +34,7 @@ def summarize_results(query: str, search_results: List[Dict[str, str]]) -> str:
|
|
| 27 |
|
| 28 |
Article:"""
|
| 29 |
|
| 30 |
-
summary = DDGS().chat(prompt, model=
|
| 31 |
return summary
|
| 32 |
except Exception as e:
|
| 33 |
return f"An error occurred during summarization: {str(e)}"
|
|
@@ -51,35 +58,77 @@ def rephrase_query(previous_response: str, new_query: str) -> str:
|
|
| 51 |
response = client.text_generation(prompt, max_new_tokens=100, temperature=0.7)
|
| 52 |
return response
|
| 53 |
|
| 54 |
-
def
|
|
|
|
| 55 |
if chat_history:
|
| 56 |
previous_response = chat_history[-1][1]
|
| 57 |
-
rephrased_query = rephrase_query(previous_response,
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
],
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
],
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
|
|
|
| 7 |
# Environment variables and configurations
|
| 8 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
| 9 |
|
| 10 |
+
MODELS = [
|
| 11 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 12 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 13 |
+
"mistralai/Mistral-Nemo-Instruct-2407",
|
| 14 |
+
"llama-3-70b"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
| 18 |
try:
|
| 19 |
results = DDGS().text(query, max_results=max_results)
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
| 23 |
|
| 24 |
+
def summarize_results(query: str, search_results: List[Dict[str, str]], model: str) -> str:
|
| 25 |
try:
|
| 26 |
context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
|
| 27 |
|
|
|
|
| 34 |
|
| 35 |
Article:"""
|
| 36 |
|
| 37 |
+
summary = DDGS().chat(prompt, model="llama-3-70b")
|
| 38 |
return summary
|
| 39 |
except Exception as e:
|
| 40 |
return f"An error occurred during summarization: {str(e)}"
|
|
|
|
| 58 |
response = client.text_generation(prompt, max_new_tokens=100, temperature=0.7)
|
| 59 |
return response
|
| 60 |
|
| 61 |
+
def respond(message, chat_history, model, temperature, num_api_calls):
|
| 62 |
+
rephrased_query = message
|
| 63 |
if chat_history:
|
| 64 |
previous_response = chat_history[-1][1]
|
| 65 |
+
rephrased_query = rephrase_query(previous_response, message)
|
| 66 |
+
|
| 67 |
+
final_summary = ""
|
| 68 |
+
for _ in range(num_api_calls):
|
| 69 |
+
search_results = get_web_search_results(rephrased_query)
|
| 70 |
+
if "error" in search_results[0]:
|
| 71 |
+
final_summary += search_results[0]["error"] + "\n"
|
| 72 |
+
else:
|
| 73 |
+
summary = summarize_results(rephrased_query, search_results, model)
|
| 74 |
+
final_summary += summary + "\n\n"
|
| 75 |
+
|
| 76 |
+
return final_summary
|
| 77 |
+
|
| 78 |
+
def initial_conversation():
|
| 79 |
+
return [
|
| 80 |
+
("Hello! How can I assist you today?", "Welcome! I'm your AI-powered Web Search and PDF Chat Assistant. I can help you find information on the web, summarize content, and analyze PDF documents. What would you like to know?")
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
css = """
|
| 84 |
+
Your custom CSS here
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
custom_placeholder = "Ask me anything about web content or uploaded PDFs..."
|
| 88 |
+
|
| 89 |
+
demo = gr.ChatInterface(
|
| 90 |
+
respond,
|
| 91 |
+
additional_inputs=[
|
| 92 |
+
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 93 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 94 |
+
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
|
| 95 |
],
|
| 96 |
+
title="AI-powered Web Search and PDF Chat Assistant",
|
| 97 |
+
description="This AI-powered Web Search and PDF Chat Assistant combines real-time web search capabilities with advanced language processing. It can analyze web content and PDF documents, providing detailed summaries and engaging in in-depth conversations about the information it discovers. Whether you're researching a topic, seeking current news, or exploring document contents, this tool offers intelligent, context-aware responses to your queries.",
|
| 98 |
+
theme=gr.themes.Soft(
|
| 99 |
+
primary_hue="orange",
|
| 100 |
+
secondary_hue="amber",
|
| 101 |
+
neutral_hue="gray",
|
| 102 |
+
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 103 |
+
).set(
|
| 104 |
+
body_background_fill_dark="#0c0505",
|
| 105 |
+
block_background_fill_dark="#0c0505",
|
| 106 |
+
block_border_width="1px",
|
| 107 |
+
block_title_background_fill_dark="#1b0f0f",
|
| 108 |
+
input_background_fill_dark="#140b0b",
|
| 109 |
+
button_secondary_background_fill_dark="#140b0b",
|
| 110 |
+
border_color_accent_dark="#1b0f0f",
|
| 111 |
+
border_color_primary_dark="#1b0f0f",
|
| 112 |
+
background_fill_secondary_dark="#0c0505",
|
| 113 |
+
color_accent_soft_dark="transparent",
|
| 114 |
+
code_background_fill_dark="#140b0b"
|
| 115 |
+
),
|
| 116 |
+
css=css,
|
| 117 |
+
examples=[
|
| 118 |
+
["Tell me about the contents of the uploaded PDFs."],
|
| 119 |
+
["What are the main topics discussed in the documents?"],
|
| 120 |
+
["Can you summarize the key points from the PDFs?"]
|
| 121 |
],
|
| 122 |
+
cache_examples=False,
|
| 123 |
+
analytics_enabled=False,
|
| 124 |
+
textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),
|
| 125 |
+
chatbot = gr.Chatbot(
|
| 126 |
+
show_copy_button=True,
|
| 127 |
+
likeable=True,
|
| 128 |
+
layout="bubble",
|
| 129 |
+
height=400,
|
| 130 |
+
value=initial_conversation()
|
| 131 |
+
)
|
| 132 |
)
|
| 133 |
|
| 134 |
+
demo.launch()
|