Spaces:
Sleeping
Sleeping
Update app.py
Browse files✔ No more crashes from 429
The retry logic absorbs rate limits gracefully.
✔ Gradio won’t spawn multiple parallel LLM calls
concurrency_limit=1 prevents overload.
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import faiss
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from huggingface_hub import InferenceClient
|
|
|
|
| 8 |
|
| 9 |
# ============================================================
|
| 10 |
# CONFIG
|
|
@@ -12,27 +14,36 @@ from huggingface_hub import InferenceClient
|
|
| 12 |
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
#LLM_MODEL = "
|
| 17 |
-
#LLM_MODEL = "meta-llama/Llama-3.1-8B-Instruct"
|
| 18 |
-
# LLM_MODEL = "mistralai/Mistral-7B-Instruct-v0.3" Does not work
|
| 19 |
-
# LLM_MODEL ="HuggingFaceH4/zephyr-7b-beta"
|
| 20 |
-
# LLM_MODEL = "NousResearch/Hermes-2-Pro-Mistral-7B" Does not work
|
| 21 |
-
|
| 22 |
-
# Great LLM
|
| 23 |
-
#LLM_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
|
| 24 |
LLM_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B"
|
| 25 |
|
| 26 |
-
# Local embedding model
|
| 27 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 28 |
|
| 29 |
-
# Load models
|
| 30 |
embedding_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 31 |
|
| 32 |
# IMPORTANT: do NOT pass model=... here
|
| 33 |
llm = InferenceClient(token=HF_TOKEN)
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# ============================================================
|
| 37 |
# PDF PROCESSING
|
| 38 |
# ============================================================
|
|
@@ -89,7 +100,7 @@ def retrieve_context(question, state, k=4):
|
|
| 89 |
|
| 90 |
|
| 91 |
# ============================================================
|
| 92 |
-
# LLM ANSWERING
|
| 93 |
# ============================================================
|
| 94 |
|
| 95 |
def generate_answer(question, context):
|
|
@@ -106,14 +117,15 @@ def generate_answer(question, context):
|
|
| 106 |
f"Question: {question}\nAnswer:"
|
| 107 |
)
|
| 108 |
|
| 109 |
-
response =
|
|
|
|
| 110 |
model=LLM_MODEL,
|
| 111 |
messages=[{"role": "user", "content": prompt}],
|
| 112 |
-
max_tokens=
|
| 113 |
temperature=0.25,
|
| 114 |
top_p=0.9,
|
| 115 |
-
frequency_penalty=0.1,
|
| 116 |
-
presence_penalty=0.0
|
| 117 |
)
|
| 118 |
|
| 119 |
return response.choices[0].message["content"].strip()
|
|
@@ -164,7 +176,7 @@ def answer_question(question, state):
|
|
| 164 |
# GRADIO UI
|
| 165 |
# ============================================================
|
| 166 |
|
| 167 |
-
with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
| 168 |
gr.Markdown("## 📄 PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 169 |
|
| 170 |
state = gr.State({"chunks": None, "index": None})
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import faiss
|
| 6 |
from PyPDF2 import PdfReader
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from huggingface_hub.errors import HfHubHTTPError
|
| 10 |
|
| 11 |
# ============================================================
|
| 12 |
# CONFIG
|
|
|
|
| 14 |
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 16 |
|
| 17 |
+
# Recommended: 7B is more stable on free HF router
|
| 18 |
+
# LLM_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
LLM_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B"
|
| 20 |
|
|
|
|
| 21 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 22 |
|
|
|
|
| 23 |
embedding_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 24 |
|
| 25 |
# IMPORTANT: do NOT pass model=... here
|
| 26 |
llm = InferenceClient(token=HF_TOKEN)
|
| 27 |
|
| 28 |
|
| 29 |
+
# ============================================================
|
| 30 |
+
# SAFE LLM CALL (prevents 429 errors)
|
| 31 |
+
# ============================================================
|
| 32 |
+
|
| 33 |
+
def safe_chat_completion(llm, **kwargs):
|
| 34 |
+
for attempt in range(5):
|
| 35 |
+
try:
|
| 36 |
+
return llm.chat.completions.create(**kwargs)
|
| 37 |
+
except HfHubHTTPError as e:
|
| 38 |
+
if "429" in str(e):
|
| 39 |
+
sleep_time = 2 ** attempt
|
| 40 |
+
print(f"[Rate limit] Retrying in {sleep_time}s...")
|
| 41 |
+
time.sleep(sleep_time)
|
| 42 |
+
else:
|
| 43 |
+
raise
|
| 44 |
+
raise RuntimeError("Too many retries after rate limits.")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
# ============================================================
|
| 48 |
# PDF PROCESSING
|
| 49 |
# ============================================================
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
# ============================================================
|
| 103 |
+
# LLM ANSWERING
|
| 104 |
# ============================================================
|
| 105 |
|
| 106 |
def generate_answer(question, context):
|
|
|
|
| 117 |
f"Question: {question}\nAnswer:"
|
| 118 |
)
|
| 119 |
|
| 120 |
+
response = safe_chat_completion(
|
| 121 |
+
llm,
|
| 122 |
model=LLM_MODEL,
|
| 123 |
messages=[{"role": "user", "content": prompt}],
|
| 124 |
+
max_tokens=1024,
|
| 125 |
temperature=0.25,
|
| 126 |
top_p=0.9,
|
| 127 |
+
frequency_penalty=0.1,
|
| 128 |
+
presence_penalty=0.0
|
| 129 |
)
|
| 130 |
|
| 131 |
return response.choices[0].message["content"].strip()
|
|
|
|
| 176 |
# GRADIO UI
|
| 177 |
# ============================================================
|
| 178 |
|
| 179 |
+
with gr.Blocks(title="PDF Q&A Chatbot", concurrency_limit=1) as demo:
|
| 180 |
gr.Markdown("## 📄 PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 181 |
|
| 182 |
state = gr.State({"chunks": None, "index": None})
|