Spaces:
Sleeping
Sleeping
abubasith86
commited on
Commit
·
5426913
1
Parent(s):
9e78176
Test
Browse files
app.py
CHANGED
@@ -1,153 +1,123 @@
|
|
1 |
import gradio as gr
|
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
|
9 |
-
"""
|
10 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
11 |
|
12 |
-
|
13 |
-
# PDF Parsing
|
14 |
-
def extract_text_from_pdf(pdf_file):
|
15 |
-
doc = pymupdf.open(pdf_file)
|
16 |
-
text = " ".join([page.get_textpage().extractTEXT() for page in doc])
|
17 |
-
return text
|
18 |
-
|
19 |
-
|
20 |
-
# Web search fallback
|
21 |
-
def search_web(query):
|
22 |
-
with DDGS() as ddgs:
|
23 |
-
results = ddgs.text(query)
|
24 |
-
if results:
|
25 |
-
return results[0]["body"]
|
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 |
|
51 |
Your goals:
|
52 |
-
-
|
53 |
-
-
|
54 |
-
-
|
55 |
-
-
|
56 |
-
- Be honest if you don’t know something.
|
57 |
-
- Always be polite, helpful, and respectful.
|
58 |
"""
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def respond(
|
62 |
-
message,
|
63 |
history: list[tuple[str, str]],
|
64 |
-
|
65 |
-
temperature=0.4,
|
66 |
-
top_p=0.1,
|
|
|
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 |
-
|
|
|
|
|
|
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
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 |
-
|
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 |
-
|
133 |
-
|
134 |
-
for
|
135 |
messages,
|
136 |
-
max_tokens=max_tokens,
|
137 |
stream=True,
|
138 |
temperature=temperature,
|
139 |
top_p=top_p,
|
|
|
140 |
):
|
141 |
-
token =
|
|
|
|
|
142 |
|
143 |
-
response += token
|
144 |
-
yield response
|
145 |
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
""
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import fitz # pymupdf
|
4 |
from duckduckgo_search import DDGS
|
5 |
from serpapi import GoogleSearch
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
|
|
|
|
|
|
|
9 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
SYSTEM_PROMPT = """
|
12 |
+
You are an intelligent and friendly AI assistant.
|
13 |
|
14 |
Your goals:
|
15 |
+
- Use provided documents to answer questions accurately.
|
16 |
+
- When the query is recent or about current events, leverage web search results.
|
17 |
+
- If nothing is provided, rely on your general knowledge.
|
18 |
+
- Always be honest, polite, and helpful.
|
|
|
|
|
19 |
"""
|
20 |
|
21 |
+
RECENT_KEYWORDS = {
|
22 |
+
"latest", "today", "current", "now", "recent", "news", "update", "price",
|
23 |
+
"who won", "what happened", "trending", "breaking", "just in", "live",
|
24 |
+
"score", "results", "weather", "forecast", "report", "market", "stocks",
|
25 |
+
"crypto", "rate", "exchange", "gold price", "happening", "event", "updates",
|
26 |
+
"hot", "viral", "announcement", "today's", "this week", "schedule", "calendar",
|
27 |
+
"launch", "drop", "release date", "opening", "closing", "deadline",
|
28 |
+
}
|
29 |
+
|
30 |
+
|
31 |
+
def extract_text_from_pdf(pdf_file) -> str:
|
32 |
+
try:
|
33 |
+
with fitz.open(pdf_file.name) as doc:
|
34 |
+
return " ".join(page.get_text() for page in doc)
|
35 |
+
except Exception as e:
|
36 |
+
return f"Failed to read PDF: {e}"
|
37 |
+
|
38 |
+
|
39 |
+
def search_web(query: str) -> str:
|
40 |
+
try:
|
41 |
+
params = {
|
42 |
+
"q": query,
|
43 |
+
"api_key": os.getenv("SERPAPI_KEY", ""), # Keep it optional and env-based
|
44 |
+
"engine": "google",
|
45 |
+
}
|
46 |
+
if params["api_key"]:
|
47 |
+
results = GoogleSearch(params).get_dict()
|
48 |
+
if "organic_results" in results:
|
49 |
+
return "\n".join(
|
50 |
+
f"{r.get('title', '')}: {r.get('snippet', '')}"
|
51 |
+
for r in results["organic_results"][:3]
|
52 |
+
)
|
53 |
+
except Exception:
|
54 |
+
pass
|
55 |
+
|
56 |
+
try:
|
57 |
+
with DDGS() as ddgs:
|
58 |
+
results = ddgs.text(query)
|
59 |
+
if results:
|
60 |
+
return results[0]["body"]
|
61 |
+
except Exception:
|
62 |
+
pass
|
63 |
+
|
64 |
+
return "No relevant web results found."
|
65 |
+
|
66 |
|
67 |
def respond(
|
68 |
+
message: str,
|
69 |
history: list[tuple[str, str]],
|
70 |
+
pdf: object = None,
|
71 |
+
temperature: float = 0.4,
|
72 |
+
top_p: float = 0.1,
|
73 |
+
max_tokens: int = 2048,
|
74 |
):
|
75 |
+
context = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
message_lower = message.lower()
|
77 |
|
78 |
+
# 1. Use PDF content if available
|
79 |
+
if pdf is not None:
|
80 |
+
context = extract_text_from_pdf(pdf)
|
81 |
+
message += f"\n\n[Document context provided below for reference:]\n{context}"
|
82 |
|
83 |
+
# 2. Use web search if query looks recent
|
84 |
+
if any(keyword in message_lower for keyword in RECENT_KEYWORDS):
|
85 |
+
web_context = search_web(message)
|
|
|
|
|
|
|
|
|
|
|
86 |
if web_context:
|
87 |
+
message += f"\n\n[Relevant web search results to help you answer]:\n{web_context}"
|
|
|
88 |
|
89 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
90 |
+
for user, assistant in history:
|
91 |
+
messages.append({"role": "user", "content": user})
|
92 |
+
messages.append({"role": "assistant", "content": assistant})
|
93 |
messages.append({"role": "user", "content": message})
|
94 |
|
95 |
+
# Stream LLM response
|
96 |
+
full_response = ""
|
97 |
+
for chunk in client.chat_completion(
|
98 |
messages,
|
|
|
99 |
stream=True,
|
100 |
temperature=temperature,
|
101 |
top_p=top_p,
|
102 |
+
max_tokens=max_tokens,
|
103 |
):
|
104 |
+
token = chunk.choices[0].delta.content or ""
|
105 |
+
full_response += token
|
106 |
+
yield full_response
|
107 |
|
|
|
|
|
108 |
|
109 |
+
with gr.Blocks() as demo:
|
110 |
+
gr.Markdown("## 💬 Smart Assistant with Web & Document Context")
|
111 |
|
112 |
+
with gr.Row():
|
113 |
+
pdf_input = gr.File(label="📄 Upload PDF (optional)", file_types=[".pdf"])
|
114 |
+
temperature = gr.Slider(0.0, 1.0, value=0.4, step=0.05, label="Temperature")
|
115 |
+
top_p = gr.Slider(0.1, 1.0, value=0.1, step=0.05, label="Top-p")
|
116 |
+
|
117 |
+
chat = gr.ChatInterface(
|
118 |
+
fn=respond,
|
119 |
+
additional_inputs=[pdf_input, temperature, top_p],
|
120 |
+
)
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch()
|