Update app.py
Browse files
app.py
CHANGED
|
@@ -12,16 +12,12 @@ from huggingface_hub import InferenceClient
|
|
| 12 |
# -----------------------------
|
| 13 |
# Config
|
| 14 |
# -----------------------------
|
| 15 |
-
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
-
# LLM (keep same default, but we will call it via chat_completion, not text_generation)
|
| 18 |
HF_LLM_MODEL = os.getenv("HF_LLM_MODEL", "mistralai/Mistral-7B-Instruct-v0.3")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
# HF_PROVIDER="together"
|
| 23 |
-
# If you leave it empty, it will use Hugging Face default provider.
|
| 24 |
-
HF_PROVIDER = os.getenv("HF_PROVIDER", "").strip() or None
|
| 25 |
|
| 26 |
EMBED_MODEL_NAME = os.getenv("EMBED_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
|
| 27 |
TOP_K = 4
|
|
@@ -81,9 +77,8 @@ def retrieve(query, embedder, index, chunks, k=TOP_K):
|
|
| 81 |
|
| 82 |
def hf_generate(client: InferenceClient, prompt: str) -> str:
|
| 83 |
"""
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
Use chat_completion (conversational) instead.
|
| 87 |
"""
|
| 88 |
resp = client.chat_completion(
|
| 89 |
model=HF_LLM_MODEL,
|
|
@@ -121,7 +116,6 @@ def on_upload(pdf_path):
|
|
| 121 |
|
| 122 |
|
| 123 |
def answer_question(index, chunks, question):
|
| 124 |
-
# FIX: gate on index/chunks, NOT on the original pdf file
|
| 125 |
if index is None or chunks is None:
|
| 126 |
return "Upload and index a PDF first."
|
| 127 |
if not question or not question.strip():
|
|
@@ -146,8 +140,7 @@ Context:
|
|
| 146 |
|
| 147 |
Answer:"""
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
# If not set, it uses Hugging Face default provider.
|
| 151 |
if HF_PROVIDER:
|
| 152 |
client = InferenceClient(provider=HF_PROVIDER, token=HF_TOKEN)
|
| 153 |
else:
|
|
@@ -169,7 +162,8 @@ with gr.Blocks(title="Agentic Document Intelligence (HF RAG)") as demo:
|
|
| 169 |
gr.Markdown(
|
| 170 |
"# 📄 Agentic Document Intelligence\n"
|
| 171 |
"Upload a PDF and ask questions (RAG) — using Hugging Face Inference API.\n\n"
|
| 172 |
-
"**
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
pdf = gr.File(label="Upload PDF", type="filepath")
|
|
|
|
| 12 |
# -----------------------------
|
| 13 |
# Config
|
| 14 |
# -----------------------------
|
| 15 |
+
HF_TOKEN = (os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN") or "").strip()
|
| 16 |
|
|
|
|
| 17 |
HF_LLM_MODEL = os.getenv("HF_LLM_MODEL", "mistralai/Mistral-7B-Instruct-v0.3")
|
| 18 |
|
| 19 |
+
# Optional: set HF_PROVIDER="together" in Space secrets if you want Together
|
| 20 |
+
HF_PROVIDER = (os.getenv("HF_PROVIDER") or "").strip() or None
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
EMBED_MODEL_NAME = os.getenv("EMBED_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
|
| 23 |
TOP_K = 4
|
|
|
|
| 77 |
|
| 78 |
def hf_generate(client: InferenceClient, prompt: str) -> str:
|
| 79 |
"""
|
| 80 |
+
Use chat_completion (conversational) because Together does not support
|
| 81 |
+
text_generation for mistralai/Mistral-7B-Instruct-v0.3.
|
|
|
|
| 82 |
"""
|
| 83 |
resp = client.chat_completion(
|
| 84 |
model=HF_LLM_MODEL,
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
def answer_question(index, chunks, question):
|
|
|
|
| 119 |
if index is None or chunks is None:
|
| 120 |
return "Upload and index a PDF first."
|
| 121 |
if not question or not question.strip():
|
|
|
|
| 140 |
|
| 141 |
Answer:"""
|
| 142 |
|
| 143 |
+
# Create client (provider optional)
|
|
|
|
| 144 |
if HF_PROVIDER:
|
| 145 |
client = InferenceClient(provider=HF_PROVIDER, token=HF_TOKEN)
|
| 146 |
else:
|
|
|
|
| 162 |
gr.Markdown(
|
| 163 |
"# 📄 Agentic Document Intelligence\n"
|
| 164 |
"Upload a PDF and ask questions (RAG) — using Hugging Face Inference API.\n\n"
|
| 165 |
+
"**If using Together:** set Space secret `HF_PROVIDER=together`.\n"
|
| 166 |
+
"**Token tip:** ensure HF token has no trailing newline."
|
| 167 |
)
|
| 168 |
|
| 169 |
pdf = gr.File(label="Upload PDF", type="filepath")
|