Spaces:
Sleeping
Sleeping
abubasith86
commited on
Commit
·
892e0a4
1
Parent(s):
699cd0d
improved
Browse files- app.py +74 -0
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import pymupdf
|
| 4 |
from duckduckgo_search import DDGS
|
|
|
|
| 5 |
|
| 6 |
"""
|
| 7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
@@ -25,6 +26,25 @@ def search_web(query):
|
|
| 25 |
return "No relevant results found on the web."
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
SYSTEM_PROMPT = """
|
| 29 |
You are an intelligent and friendly AI assistant.
|
| 30 |
|
|
@@ -37,6 +57,7 @@ Your goals:
|
|
| 37 |
- Always be polite, helpful, and respectful.
|
| 38 |
"""
|
| 39 |
|
|
|
|
| 40 |
def respond(
|
| 41 |
message,
|
| 42 |
history: list[tuple[str, str]],
|
|
@@ -45,6 +66,53 @@ def respond(
|
|
| 45 |
top_p,
|
| 46 |
):
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 49 |
|
| 50 |
for val in history:
|
|
@@ -53,6 +121,12 @@ def respond(
|
|
| 53 |
if val[1]:
|
| 54 |
messages.append({"role": "assistant", "content": val[1]})
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
messages.append({"role": "user", "content": message})
|
| 57 |
|
| 58 |
response = ""
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import pymupdf
|
| 4 |
from duckduckgo_search import DDGS
|
| 5 |
+
from serpapi import GoogleSearch
|
| 6 |
|
| 7 |
"""
|
| 8 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
|
|
| 26 |
return "No relevant results found on the web."
|
| 27 |
|
| 28 |
|
| 29 |
+
def google_search(query):
|
| 30 |
+
params = {
|
| 31 |
+
"q": query,
|
| 32 |
+
"api_key": "b11d4a3660600e7e7f481b3288f107fbf993389a20125b0a97ebe7ab207854a5", # Replace this with your real key
|
| 33 |
+
"engine": "google",
|
| 34 |
+
}
|
| 35 |
+
search = GoogleSearch(params)
|
| 36 |
+
results = search.get_dict()
|
| 37 |
+
if "organic_results" in results:
|
| 38 |
+
# Combine top 3 results
|
| 39 |
+
summaries = []
|
| 40 |
+
for res in results["organic_results"][:3]:
|
| 41 |
+
title = res.get("title", "")
|
| 42 |
+
snippet = res.get("snippet", "")
|
| 43 |
+
summaries.append(f"{title}: {snippet}")
|
| 44 |
+
return "\n".join(summaries)
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
|
| 48 |
SYSTEM_PROMPT = """
|
| 49 |
You are an intelligent and friendly AI assistant.
|
| 50 |
|
|
|
|
| 57 |
- Always be polite, helpful, and respectful.
|
| 58 |
"""
|
| 59 |
|
| 60 |
+
|
| 61 |
def respond(
|
| 62 |
message,
|
| 63 |
history: list[tuple[str, str]],
|
|
|
|
| 66 |
top_p,
|
| 67 |
):
|
| 68 |
|
| 69 |
+
recent_keywords = [
|
| 70 |
+
"latest",
|
| 71 |
+
"today",
|
| 72 |
+
"current",
|
| 73 |
+
"now",
|
| 74 |
+
"recent",
|
| 75 |
+
"news",
|
| 76 |
+
"update",
|
| 77 |
+
"price",
|
| 78 |
+
"who won",
|
| 79 |
+
"what happened",
|
| 80 |
+
"trending",
|
| 81 |
+
"breaking",
|
| 82 |
+
"just in",
|
| 83 |
+
"new release",
|
| 84 |
+
"live",
|
| 85 |
+
"score",
|
| 86 |
+
"results",
|
| 87 |
+
"weather",
|
| 88 |
+
"forecast",
|
| 89 |
+
"report",
|
| 90 |
+
"market",
|
| 91 |
+
"stocks",
|
| 92 |
+
"crypto",
|
| 93 |
+
"rate",
|
| 94 |
+
"exchange",
|
| 95 |
+
"gold price",
|
| 96 |
+
"happening",
|
| 97 |
+
"event",
|
| 98 |
+
"updates",
|
| 99 |
+
"hot",
|
| 100 |
+
"viral",
|
| 101 |
+
"announcement",
|
| 102 |
+
"today's",
|
| 103 |
+
"this week",
|
| 104 |
+
"schedule",
|
| 105 |
+
"calendar",
|
| 106 |
+
"launch",
|
| 107 |
+
"drop",
|
| 108 |
+
"release date",
|
| 109 |
+
"opening",
|
| 110 |
+
"closing",
|
| 111 |
+
"deadline",
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
message_lower = message.lower()
|
| 115 |
+
|
| 116 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 117 |
|
| 118 |
for val in history:
|
|
|
|
| 121 |
if val[1]:
|
| 122 |
messages.append({"role": "assistant", "content": val[1]})
|
| 123 |
|
| 124 |
+
if any(kw in message_lower for kw in recent_keywords):
|
| 125 |
+
web_context = google_search(message)
|
| 126 |
+
if web_context:
|
| 127 |
+
# Inject web context as part of the user's query
|
| 128 |
+
message = f"{message}\n\n[Relevant web search results to help you answer]:\n{web_context}"
|
| 129 |
+
|
| 130 |
messages.append({"role": "user", "content": message})
|
| 131 |
|
| 132 |
response = ""
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
huggingface_hub==0.25.2
|
| 2 |
duckduckgo_search
|
| 3 |
-
pymupdf
|
|
|
|
|
|
| 1 |
huggingface_hub==0.25.2
|
| 2 |
duckduckgo_search
|
| 3 |
+
pymupdf
|
| 4 |
+
google-search-results
|