Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,178 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from langchain_community.vectorstores import Chroma
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.docstore.document import Document
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
import tempfile
|
| 9 |
+
import shutil
|
| 10 |
|
| 11 |
+
# Global state
|
| 12 |
+
retriever = None
|
| 13 |
+
db_path = "./chromadb"
|
| 14 |
|
| 15 |
+
# 1. FASTEST Embedding Model (loads in 2s)
|
| 16 |
+
embeddings = HuggingFaceEmbeddings(
|
| 17 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 18 |
+
model_kwargs={'device': 'cpu'}
|
| 19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# 2. FASTEST Generation Model (google/flan-t5-small = 250MB)
|
| 22 |
+
generator = pipeline(
|
| 23 |
+
"text2text-generation",
|
| 24 |
+
model="google/flan-t5-small",
|
| 25 |
+
device=-1 # CPU
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
def process_files(files):
|
| 29 |
+
"""Convert uploaded files to documents"""
|
| 30 |
+
global retriever
|
| 31 |
+
docs = []
|
| 32 |
+
|
| 33 |
+
for file in files:
|
| 34 |
+
# Save temp file
|
| 35 |
+
temp_path = f"/tmp/{file.name}"
|
| 36 |
+
file.save(temp_path)
|
| 37 |
+
|
| 38 |
+
# Read content
|
| 39 |
+
try:
|
| 40 |
+
with open(temp_path, "r", encoding="utf-8") as f:
|
| 41 |
+
content = f.read()
|
| 42 |
+
except:
|
| 43 |
+
content = ""
|
| 44 |
+
|
| 45 |
+
if content.strip():
|
| 46 |
+
# Fast chunking
|
| 47 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 48 |
+
chunk_size=800,
|
| 49 |
+
chunk_overlap=100
|
| 50 |
+
)
|
| 51 |
+
chunks = splitter.split_text(content)
|
| 52 |
+
|
| 53 |
+
for i, chunk in enumerate(chunks):
|
| 54 |
+
docs.append(Document(
|
| 55 |
+
page_content=chunk,
|
| 56 |
+
metadata={"source": file.name, "chunk": i}
|
| 57 |
+
))
|
| 58 |
+
|
| 59 |
+
# Build FAST vector DB
|
| 60 |
+
if docs:
|
| 61 |
+
# Clear old DB
|
| 62 |
+
if os.path.exists(db_path):
|
| 63 |
+
shutil.rmtree(db_path)
|
| 64 |
+
|
| 65 |
+
db = Chroma.from_documents(
|
| 66 |
+
docs,
|
| 67 |
+
embeddings,
|
| 68 |
+
persist_directory=db_path
|
| 69 |
+
)
|
| 70 |
+
retriever = db.as_retriever(search_kwargs={"k": 4})
|
| 71 |
+
return f"β
Loaded {len(docs)} chunks from {len(files)} files!"
|
| 72 |
+
|
| 73 |
+
return "β No content found"
|
| 74 |
|
| 75 |
+
def smart_answer(question):
|
| 76 |
+
"""Generate smart RAG answer"""
|
| 77 |
+
global retriever
|
| 78 |
+
|
| 79 |
+
if not retriever:
|
| 80 |
+
return "π **First upload your text files!**"
|
| 81 |
+
|
| 82 |
+
# Get relevant docs FAST
|
| 83 |
+
docs = retriever.get_relevant_documents(question)
|
| 84 |
+
if not docs:
|
| 85 |
+
return "β No relevant info found. Try different words."
|
| 86 |
+
|
| 87 |
+
# Build context (first 600 chars each)
|
| 88 |
+
context = "\n\n".join([d.page_content[:600] for d in docs])
|
| 89 |
+
|
| 90 |
+
# SMART prompt
|
| 91 |
+
prompt = f"""Use ONLY this context to answer:
|
| 92 |
|
| 93 |
+
CONTEXT:
|
| 94 |
+
{context}
|
| 95 |
|
| 96 |
+
QUESTION: {question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
ANSWER:"""
|
| 99 |
+
|
| 100 |
+
# Generate FAST
|
| 101 |
+
result = generator(
|
| 102 |
+
prompt,
|
| 103 |
+
max_new_tokens=200,
|
| 104 |
+
do_sample=False,
|
| 105 |
+
temperature=0.1
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
answer = result[0]["generated_text"].split("ANSWER:")[-1].strip()
|
| 109 |
+
|
| 110 |
+
# Sources
|
| 111 |
+
sources = list(set(d.metadata["source"] for d in docs))
|
| 112 |
+
|
| 113 |
+
return f"""
|
| 114 |
+
<div style='font-family: -apple-system, BlinkMacSystemFont, sans-serif; max-width: 800px;'>
|
| 115 |
+
<div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 116 |
+
color: white; padding: 20px; border-radius: 15px; text-align: center; margin-bottom: 20px;'>
|
| 117 |
+
<h1 style='margin: 0; font-size: 28px;'>π€ RAG Chatbot</h1>
|
| 118 |
+
<p style='margin: 5px 0 0 0; opacity: 0.9;'>Your documents β Smart answers</p>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
<div style='background: white; padding: 25px; border-radius: 15px;
|
| 122 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1);'>
|
| 123 |
+
<div style='background: #f8f9fa; padding: 15px; border-radius: 10px; margin-bottom: 20px;'>
|
| 124 |
+
<p style='margin: 0 0 5px 0; font-weight: bold; color: #495057;'>β Your Question:</p>
|
| 125 |
+
<p style='margin: 0; color: #6c757d; font-size: 15px;'>{question}</p>
|
| 126 |
+
</div>
|
| 127 |
+
|
| 128 |
+
<div style='background: linear-gradient(135deg, #11998e, #38ef7d);
|
| 129 |
+
color: white; padding: 20px; border-radius: 12px; margin-bottom: 15px;'>
|
| 130 |
+
<p style='margin: 0 0 10px 0; font-weight: bold; font-size: 18px;'>π‘ Answer:</p>
|
| 131 |
+
<div style='background: rgba(255,255,255,0.2); padding: 15px; border-radius: 8px;
|
| 132 |
+
line-height: 1.6; font-size: 16px;'>{answer}</div>
|
| 133 |
+
</div>
|
| 134 |
+
|
| 135 |
+
<div style='font-size: 14px; color: #6c757d;'>
|
| 136 |
+
π <strong>Sources:</strong> {', '.join(sources)}
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
</div>
|
| 140 |
+
"""
|
| 141 |
|
| 142 |
+
# Gradio UI - SIMPLE & FAST
|
| 143 |
+
with gr.Blocks(
|
| 144 |
+
title="My RAG Chatbot",
|
| 145 |
+
theme=gr.themes.Soft()
|
| 146 |
+
) as demo:
|
| 147 |
+
|
| 148 |
+
gr.Markdown("# π€ **My RAG Chatbot**")
|
| 149 |
+
gr.Markdown("*Upload text files β Ask questions β Get smart answers*")
|
| 150 |
+
|
| 151 |
+
with gr.Tab("π Upload Files"):
|
| 152 |
+
files = gr.File(file_count="multiple", file_types=[".txt", ".pdf"])
|
| 153 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 154 |
+
upload_btn = gr.Button("π Build Knowledge Base", variant="primary")
|
| 155 |
+
|
| 156 |
+
upload_btn.click(
|
| 157 |
+
fn=process_files,
|
| 158 |
+
inputs=files,
|
| 159 |
+
outputs=status
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
with gr.Tab("π¬ Ask Questions"):
|
| 163 |
+
question = gr.Textbox(
|
| 164 |
+
lines=3,
|
| 165 |
+
placeholder="Ask anything about your documents...",
|
| 166 |
+
label="Your Question"
|
| 167 |
+
)
|
| 168 |
+
answer = gr.HTML()
|
| 169 |
+
ask_btn = gr.Button("π‘ Get Smart Answer", variant="primary")
|
| 170 |
+
|
| 171 |
+
ask_btn.click(
|
| 172 |
+
fn=smart_answer,
|
| 173 |
+
inputs=question,
|
| 174 |
+
outputs=answer
|
| 175 |
+
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
demo.launch()
|