abubasith86 commited on
Commit
a9c7e25
·
1 Parent(s): 5426913

Reviked changes

Browse files
Files changed (1) hide show
  1. app.py +132 -89
app.py CHANGED
@@ -1,123 +1,166 @@
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()
 
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
+ - Answer user questions clearly and concisely.
53
+ - If a PDF document is provided, use its content to give informed answers.
54
+ - For questions about recent or live topics (e.g., news, prices, events), you may perform a web search and summarize the result.
55
+ - If no document or web context is available, still try to help using general knowledge.
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
+ max_tokens=2048,
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
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
 
 
117
 
118
+ for val in history:
119
+ if val[0]:
120
+ messages.append({"role": "user", "content": val[0]})
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 = ""
133
+
134
+ for message in client.chat_completion(
135
  messages,
136
+ max_tokens=max_tokens,
137
  stream=True,
138
  temperature=temperature,
139
  top_p=top_p,
 
140
  ):
141
+ token = message.choices[0].delta.content
 
 
 
142
 
143
+ response += token
144
+ yield response
145
 
 
 
 
 
146
 
147
+ """
148
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
149
+ """
150
+ demo = gr.ChatInterface(
151
+ respond,
152
+ additional_inputs=[
153
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
154
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
155
+ gr.Slider(
156
+ minimum=0.1,
157
+ maximum=1.0,
158
+ value=0.95,
159
+ step=0.05,
160
+ label="Top-p (nucleus sampling)",
161
+ ),
162
+ ],
163
+ )
164
 
165
  if __name__ == "__main__":
166
  demo.launch()