Spaces:
Running
Running
Thomas Stone
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,46 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -23,10 +58,13 @@ def respond(
|
|
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,31 +73,19 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a knowledgeable
|
50 |
-
|
51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
],
|
61 |
)
|
62 |
|
63 |
-
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import fitz # PyMuPDF
|
3 |
+
import faiss
|
4 |
+
import numpy as np
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
from huggingface_hub import InferenceClient
|
7 |
|
8 |
+
# Load embedding model
|
9 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
10 |
+
|
11 |
+
# Function to extract text from PDFs
|
12 |
+
def extract_text_from_pdf(pdf_path):
|
13 |
+
doc = fitz.open(pdf_path)
|
14 |
+
text = ""
|
15 |
+
for page in doc:
|
16 |
+
text += page.get_text() + "\n"
|
17 |
+
return text
|
18 |
+
|
19 |
+
# Load and process PDFs
|
20 |
+
pdf_files = ["eligibility_guidelines.pdf", "public_assistance_rules.pdf"] # Add PDF filenames
|
21 |
+
all_text = ""
|
22 |
+
for pdf in pdf_files:
|
23 |
+
all_text += extract_text_from_pdf(pdf)
|
24 |
+
|
25 |
+
# Split into chunks
|
26 |
+
chunk_size = 500
|
27 |
+
chunks = [all_text[i:i+chunk_size] for i in range(0, len(all_text), chunk_size)]
|
28 |
+
|
29 |
+
# Generate embeddings
|
30 |
+
embeddings = np.array([model.encode(chunk) for chunk in chunks])
|
31 |
+
|
32 |
+
# Create FAISS index
|
33 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
34 |
+
index.add(embeddings)
|
35 |
+
|
36 |
+
# Function to retrieve relevant text
|
37 |
+
def search_pdf(query, top_k=3):
|
38 |
+
query_embedding = model.encode(query).reshape(1, -1)
|
39 |
+
distances, indices = index.search(query_embedding, top_k)
|
40 |
+
return "\n\n".join([chunks[i] for i in indices[0]])
|
41 |
|
42 |
+
# Hugging Face LLM Client
|
43 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
44 |
|
45 |
def respond(
|
46 |
message,
|
|
|
58 |
if val[1]:
|
59 |
messages.append({"role": "assistant", "content": val[1]})
|
60 |
|
61 |
+
# Search for relevant text in PDFs
|
62 |
+
pdf_context = search_pdf(message)
|
63 |
+
messages.append({"role": "system", "content": f"Relevant PDF Info:\n{pdf_context}"})
|
64 |
+
|
65 |
messages.append({"role": "user", "content": message})
|
66 |
|
67 |
response = ""
|
|
|
68 |
for message in client.chat_completion(
|
69 |
messages,
|
70 |
max_tokens=max_tokens,
|
|
|
73 |
top_p=top_p,
|
74 |
):
|
75 |
token = message.choices[0].delta.content
|
|
|
76 |
response += token
|
77 |
yield response
|
78 |
|
79 |
+
# Gradio Chat Interface
|
|
|
|
|
|
|
80 |
demo = gr.ChatInterface(
|
81 |
respond,
|
82 |
additional_inputs=[
|
83 |
+
gr.Textbox(value="You are a knowledgeable chatbot assisting Colorado case workers with Medicaid, SNAP, TANF, CHP+, and other programs.", label="System message"),
|
|
|
84 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
85 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
86 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
],
|
88 |
)
|
89 |
|
|
|
90 |
if __name__ == "__main__":
|
91 |
demo.launch()
|