Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,99 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
response = ""
|
29 |
|
|
|
30 |
for message in client.chat_completion(
|
31 |
-
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
34 |
temperature=temperature,
|
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
|
50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
import faiss
|
5 |
+
import numpy as np
|
6 |
+
import pdfplumber
|
7 |
+
import re
|
8 |
|
9 |
+
# Initialize the InferenceClient
|
|
|
|
|
10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
11 |
|
12 |
+
# Function to extract text from PDFs
|
13 |
+
def extract_text_from_pdf(pdf_path):
|
14 |
+
text = ""
|
15 |
+
with pdfplumber.open(pdf_path) as pdf:
|
16 |
+
for page in pdf.pages:
|
17 |
+
page_text = page.extract_text()
|
18 |
+
if page_text:
|
19 |
+
text += page_text
|
20 |
+
return text
|
21 |
|
22 |
+
# Clean the extracted text
|
23 |
+
def clean_extracted_text(text):
|
24 |
+
# Removing any unnecessary characters, such as file paths and non-text data
|
25 |
+
cleaned_text = re.sub(r'file://[^\n]*', '', text) # Remove file paths
|
26 |
+
cleaned_text = re.sub(r'\d{1,2}/\d{1,2}/\d{4}', '', cleaned_text) # Remove dates
|
27 |
+
cleaned_text = re.sub(r'[^a-zA-Z0-9\u0600-\u06FF\s\u00C0-\u00FF]+', '', cleaned_text) # Keep Arabic and basic text
|
28 |
+
return cleaned_text.strip()
|
|
|
|
|
29 |
|
30 |
+
# Path to the uploaded PDF file
|
31 |
+
pdf_path = "/content/Noor-Book.com القاموس عربي فرنسي بالمصطلحات العلمية و الصور 3 (1).pdf"
|
|
|
|
|
|
|
32 |
|
33 |
+
# Extract and clean text from the provided PDF
|
34 |
+
pdf_text = extract_text_from_pdf(pdf_path)
|
35 |
+
cleaned_text = clean_extracted_text(pdf_text)
|
36 |
|
37 |
+
# Split the cleaned text into chunks for processing
|
38 |
+
def chunk_text(text, chunk_size=300):
|
39 |
+
sentences = text.split('. ')
|
40 |
+
chunks, current_chunk = [], ""
|
41 |
+
for sentence in sentences:
|
42 |
+
if len(current_chunk) + len(sentence) <= chunk_size:
|
43 |
+
current_chunk += sentence + ". "
|
44 |
+
else:
|
45 |
+
chunks.append(current_chunk.strip())
|
46 |
+
current_chunk = sentence + ". "
|
47 |
+
if current_chunk:
|
48 |
+
chunks.append(current_chunk.strip())
|
49 |
+
return chunks
|
50 |
+
|
51 |
+
# Chunk the cleaned text
|
52 |
+
chunked_text = chunk_text(cleaned_text)
|
53 |
+
|
54 |
+
# Load pre-trained Sentence Transformer model for embeddings
|
55 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
56 |
+
index = faiss.IndexFlatL2(model.get_sentence_embedding_dimension())
|
57 |
+
|
58 |
+
# Generate embeddings for the chunks
|
59 |
+
embeddings = model.encode(chunked_text, convert_to_tensor=True).detach().cpu().numpy()
|
60 |
+
index.add(embeddings)
|
61 |
+
|
62 |
+
# Function to generate response from the model
|
63 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
64 |
+
# Step 1: Retrieve relevant chunks based on the user query
|
65 |
+
query_embedding = model.encode([message], convert_to_tensor=True).detach().cpu().numpy()
|
66 |
+
k = 5 # Number of relevant chunks to retrieve
|
67 |
+
_, indices = index.search(query_embedding, k)
|
68 |
+
relevant_chunks = " ".join([chunked_text[idx] for idx in indices[0]])
|
69 |
+
|
70 |
+
# Step 2: Create prompt for the language model
|
71 |
+
prompt = f"{system_message}\n\nUser Query: {message}\n\nRelevant Information: {relevant_chunks}"
|
72 |
response = ""
|
73 |
|
74 |
+
# Step 3: Generate response using the HuggingFace model
|
75 |
for message in client.chat_completion(
|
76 |
+
[{"role": "system", "content": system_message}, {"role": "user", "content": message}],
|
77 |
max_tokens=max_tokens,
|
78 |
stream=True,
|
79 |
temperature=temperature,
|
80 |
top_p=top_p,
|
81 |
):
|
82 |
token = message.choices[0].delta.content
|
|
|
83 |
response += token
|
84 |
yield response
|
85 |
|
86 |
+
# Create the Gradio interface with additional inputs
|
|
|
|
|
|
|
87 |
demo = gr.ChatInterface(
|
88 |
respond,
|
89 |
additional_inputs=[
|
90 |
+
gr.Textbox(value="You are a helpful and empathetic mental health assistant.", label="System message"),
|
91 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
92 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
93 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
],
|
95 |
)
|
96 |
|
97 |
+
# Launch the Gradio interface
|
98 |
if __name__ == "__main__":
|
99 |
+
demo.launch()
|