Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,151 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
-
history
|
| 8 |
system_message,
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
"""
|
| 15 |
-
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
|
| 16 |
-
"""
|
| 17 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
messages
|
|
|
|
|
|
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
| 27 |
-
for
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
| 30 |
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
choices = message.choices
|
| 35 |
token = ""
|
| 36 |
-
if
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 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 |
with gr.Blocks() as demo:
|
| 63 |
with gr.Sidebar():
|
| 64 |
gr.LoginButton()
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
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 |
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# GLOBALS (simple in-memory store)
|
| 9 |
+
# -----------------------------
|
| 10 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 11 |
+
vector_store = None
|
| 12 |
+
documents = []
|
| 13 |
|
| 14 |
+
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# DOCUMENT PROCESSING
|
| 17 |
+
# -----------------------------
|
| 18 |
+
def read_file(file):
|
| 19 |
+
text = ""
|
| 20 |
+
|
| 21 |
+
if file.name.endswith(".txt"):
|
| 22 |
+
text = file.read().decode("utf-8")
|
| 23 |
+
|
| 24 |
+
elif file.name.endswith(".pdf"):
|
| 25 |
+
import PyPDF2
|
| 26 |
+
reader = PyPDF2.PdfReader(file)
|
| 27 |
+
for page in reader.pages:
|
| 28 |
+
text += page.extract_text() + "\n"
|
| 29 |
+
|
| 30 |
+
elif file.name.endswith(".docx"):
|
| 31 |
+
import docx
|
| 32 |
+
doc = docx.Document(file)
|
| 33 |
+
for para in doc.paragraphs:
|
| 34 |
+
text += para.text + "\n"
|
| 35 |
+
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def chunk_text(text, chunk_size=500):
|
| 40 |
+
chunks = []
|
| 41 |
+
for i in range(0, len(text), chunk_size):
|
| 42 |
+
chunks.append(text[i:i + chunk_size])
|
| 43 |
+
return chunks
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def process_document(file):
|
| 47 |
+
global vector_store, documents
|
| 48 |
+
|
| 49 |
+
text = read_file(file)
|
| 50 |
+
chunks = chunk_text(text)
|
| 51 |
+
|
| 52 |
+
embeddings = embedder.encode(chunks)
|
| 53 |
+
|
| 54 |
+
dim = embeddings.shape[1]
|
| 55 |
+
vector_store = faiss.IndexFlatL2(dim)
|
| 56 |
+
vector_store.add(np.array(embeddings))
|
| 57 |
+
|
| 58 |
+
documents = chunks
|
| 59 |
+
|
| 60 |
+
return f"Document processed: {len(chunks)} chunks created."
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# -----------------------------
|
| 64 |
+
# RETRIEVAL
|
| 65 |
+
# -----------------------------
|
| 66 |
+
def retrieve(query, k=3):
|
| 67 |
+
global vector_store, documents
|
| 68 |
+
|
| 69 |
+
if vector_store is None:
|
| 70 |
+
return ""
|
| 71 |
+
|
| 72 |
+
query_embedding = embedder.encode([query])
|
| 73 |
+
distances, indices = vector_store.search(np.array(query_embedding), k)
|
| 74 |
+
|
| 75 |
+
retrieved_chunks = [documents[i] for i in indices[0]]
|
| 76 |
+
return "\n\n".join(retrieved_chunks)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# -----------------------------
|
| 80 |
+
# CHAT FUNCTION
|
| 81 |
+
# -----------------------------
|
| 82 |
def respond(
|
| 83 |
message,
|
| 84 |
+
history,
|
| 85 |
system_message,
|
| 86 |
max_tokens,
|
| 87 |
temperature,
|
| 88 |
top_p,
|
| 89 |
hf_token: gr.OAuthToken,
|
| 90 |
):
|
|
|
|
|
|
|
|
|
|
| 91 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 92 |
|
| 93 |
+
# 🔥 Retrieve relevant document context
|
| 94 |
+
context = retrieve(message)
|
| 95 |
|
| 96 |
+
augmented_message = f"""
|
| 97 |
+
Use the following context to answer the question.
|
| 98 |
+
|
| 99 |
+
Context:
|
| 100 |
+
{context}
|
| 101 |
+
|
| 102 |
+
Question:
|
| 103 |
+
{message}
|
| 104 |
+
"""
|
| 105 |
|
| 106 |
+
messages = [{"role": "system", "content": system_message}]
|
| 107 |
+
messages.extend(history)
|
| 108 |
+
messages.append({"role": "user", "content": augmented_message})
|
| 109 |
|
| 110 |
response = ""
|
| 111 |
|
| 112 |
+
for chunk in client.chat_completion(
|
| 113 |
messages,
|
| 114 |
max_tokens=max_tokens,
|
| 115 |
stream=True,
|
| 116 |
temperature=temperature,
|
| 117 |
top_p=top_p,
|
| 118 |
):
|
|
|
|
| 119 |
token = ""
|
| 120 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 121 |
+
token = chunk.choices[0].delta.content
|
| 122 |
|
| 123 |
response += token
|
| 124 |
yield response
|
| 125 |
|
| 126 |
|
| 127 |
+
# -----------------------------
|
| 128 |
+
# UI
|
| 129 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
with gr.Blocks() as demo:
|
| 131 |
with gr.Sidebar():
|
| 132 |
gr.LoginButton()
|
| 133 |
+
file_upload = gr.File(label="Upload Document")
|
| 134 |
+
process_btn = gr.Button("Process Document")
|
| 135 |
+
status = gr.Textbox(label="Status")
|
| 136 |
+
|
| 137 |
+
chatbot = gr.ChatInterface(
|
| 138 |
+
respond,
|
| 139 |
+
additional_inputs=[
|
| 140 |
+
gr.Textbox(value="You are a helpful research assistant.", label="System message"),
|
| 141 |
+
gr.Slider(1, 2048, value=512, step=1, label="Max tokens"),
|
| 142 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 143 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
|
| 144 |
+
],
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
process_btn.click(process_document, inputs=file_upload, outputs=status)
|
| 148 |
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
demo.launch()
|