abubasith86 commited on
Commit
188ef10
·
1 Parent(s): f87d675

File upload added

Browse files
Files changed (2) hide show
  1. app.py +60 -10
  2. requirements.txt +4 -1
app.py CHANGED
@@ -1,10 +1,28 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  """
5
  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
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  def respond(
@@ -14,19 +32,51 @@ def respond(
14
  max_tokens,
15
  temperature,
16
  top_p,
 
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
-
30
  for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
@@ -35,8 +85,7 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.choices[0].delta.content
38
-
39
- response += token
40
  yield response
41
 
42
 
@@ -56,6 +105,7 @@ demo = gr.ChatInterface(
56
  step=0.05,
57
  label="Top-p (nucleus sampling)",
58
  ),
 
59
  ],
60
  )
61
 
 
1
  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
8
  """
9
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
10
+
11
+
12
+ # PDF Parsing
13
+ def extract_text_from_pdf(pdf_file):
14
+ doc = pymupdf.open(pdf_file)
15
+ text = " ".join([page.get_textpage().extractTEXT() for page in doc])
16
+ return text
17
+
18
+
19
+ # Web search fallback
20
+ def search_web(query):
21
+ with DDGS() as ddgs:
22
+ results = ddgs.text(query)
23
+ if results:
24
+ return results[0]["body"]
25
+ return "No relevant results found on the web."
26
 
27
 
28
  def respond(
 
32
  max_tokens,
33
  temperature,
34
  top_p,
35
+ pdf_file=None,
36
  ):
37
+ recent_keywords = [
38
+ "latest",
39
+ "today",
40
+ "current",
41
+ "now",
42
+ "recent",
43
+ "news",
44
+ "update",
45
+ "price",
46
+ "who won",
47
+ "what happened",
48
+ ]
49
+ message_lower = message.lower()
50
 
51
+ # Check if this is a recent/live query
52
+ if any(kw in message_lower for kw in recent_keywords):
53
+ web_result = search_web(message)
54
+ if web_result:
55
+ yield f"[Answer from Web Search]\n{web_result}"
56
+ return
57
 
58
+ # Check if PDF was uploaded
59
+ context_text = ""
60
+ if pdf_file is not None:
61
+ try:
62
+ context_text = extract_text_from_pdf(pdf_file.name)
63
+ except Exception as e:
64
+ yield f"[Error reading PDF: {str(e)}]"
65
+
66
+ # Try answering from PDF if context exists
67
+ if context_text.strip():
68
+ message = f"Given the following document context:\n\n{context_text[:2000]}\n\nAnswer the question:\n{message}"
69
+
70
+ # Fallback to LLM (Zephyr)
71
+ messages = [{"role": "system", "content": system_message}]
72
+ for user, assistant in history:
73
+ if user:
74
+ messages.append({"role": "user", "content": user})
75
+ if assistant:
76
+ messages.append({"role": "assistant", "content": assistant})
77
  messages.append({"role": "user", "content": message})
78
 
79
  response = ""
 
80
  for message in client.chat_completion(
81
  messages,
82
  max_tokens=max_tokens,
 
85
  top_p=top_p,
86
  ):
87
  token = message.choices[0].delta.content
88
+ response += token if token is not None else ""
 
89
  yield response
90
 
91
 
 
105
  step=0.05,
106
  label="Top-p (nucleus sampling)",
107
  ),
108
+ gr.File(label="Upload a PDF", file_types=[".pdf"]),
109
  ],
110
  )
111
 
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- huggingface_hub==0.25.2
 
 
 
 
1
+ huggingface_hub==0.25.2
2
+ duckduckgo_search
3
+ pymupdf
4
+ gradio