UCC_AI / app.py
Anaconda024's picture
Update app.py
8bda26b verified
Raw History Blame Contribute Delete
15.1 kB
import os
import gradio as gr
import faiss
import numpy as np
import requests
import docx2txt
from bs4 import BeautifulSoup
from sentence_transformers import SentenceTransformer
from huggingface_hub import InferenceClient
from PyPDF2 import PdfReader
from huggingface_hub import HfApi
# ==========================
# CONFIG
# ==========================
# Folder where your company docs are stored inside the Space
DOCS_FOLDER = "docs"
# Website to crawl
COMPANY_URL = "https://www.ucc.co.ug/about-us/"
# Embedding model
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
# Globals
index = None
chunks = []
# ==========================
# Helpers
# ==========================
def load_file(filepath):
text = ""
if filepath.endswith(".pdf"):
reader = PdfReader(filepath)
for page in reader.pages:
text += page.extract_text() + "\n"
elif filepath.endswith(".docx"):
text = docx2txt.process(filepath)
else:
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
text = f.read()
return text
def crawl_website(url, max_pages=3):
visited, to_visit, texts = set(), [url], []
while to_visit and len(visited) < max_pages:
u = to_visit.pop(0)
if u in visited:
continue
try:
r = requests.get(u, timeout=10)
r.raise_for_status() # Raise an exception for bad status codes
soup = BeautifulSoup(r.text, "html.parser")
for p in soup.find_all("p"):
texts.append(p.get_text())
# Basic link discovery (adjust if needed)
for a in soup.find_all("a", href=True):
link = a["href"]
if link.startswith("/"):
link = url.rstrip("/") + link
# Check if the link is likely within the same domain
if url in link and link not in visited and link not in to_visit:
to_visit.append(link)
except Exception as e:
print(f"Crawl error for {u}: {e}")
visited.add(u)
return "\n".join(texts)
def build_index():
global index, chunks
print("Building index...")
# Initialize even if empty to prevent NoneType errors later
index = None
chunks = []
documents = []
# 1. Load company docs from folder
if os.path.exists(DOCS_FOLDER):
print(f"Loading documents from {DOCS_FOLDER}...")
for fname in os.listdir(DOCS_FOLDER):
fpath = os.path.join(DOCS_FOLDER, fname)
try:
text = load_file(fpath)
documents.append(text)
print(f" - Loaded {fname}")
except Exception as e:
print(f" - Failed to load {fname}: {e}")
else:
print(f"Docs folder '{DOCS_FOLDER}' not found. Skipping local document loading.")
# 2. Crawl website
print(f"Crawling website: {COMPANY_URL}")
try:
web_text = crawl_website(COMPANY_URL)
if web_text:
documents.append(web_text)
print(f" - Successfully crawled {COMPANY_URL}")
else:
print(f" - No text found or error occurred while crawling {COMPANY_URL}")
except Exception as e:
print(f" - Error during crawling: {e}")
# 3. Chunk docs
print("Chunking documents...")
chunks = []
for doc in documents:
# Simple chunking by character count
for i in range(0, len(doc), 500):
chunks.append(doc[i:i+500])
# 4. Embed & index
if chunks:
print(f"Embedding {len(chunks)} chunks...")
try:
embeddings = embedder.encode(chunks, convert_to_numpy=True)
dim = embeddings.shape[1]
index = faiss.IndexFlatL2(dim)
index.add(embeddings.astype('float32')) # Ensure float32 for FAISS
print(f"Indexed {len(chunks)} chunks successfully.")
except Exception as e:
print(f"Error during embedding/indexing: {e}")
index = None
else:
print("No text chunks found. Index will be empty.")
index = faiss.IndexFlatL2(384) # Dummy index with correct dim for all-MiniLM-L6-v2
def is_model_accessible(token: str, model_id: str) -> bool:
"""
Checks if the model exists and if the user has permission to access it via the Hub API.
"""
try:
api = HfApi()
# This call requires a valid token and checks permissions
model_info = api.model_info(model_id, token=token)
print(f"Model '{model_id}' is accessible. Model info retrieved.")
# print(f"Model info snippet: {model_info}") # Uncomment for more details
return True
except Exception as e:
print(f"Model accessibility check failed for '{model_id}': {e}")
# Check specific error types if needed (e.g., HTTP 401, 403, 404)
return False
def is_token_valid(token: str) -> bool:
"""
Performs a basic check if the HF token is valid by calling the /api/whoami-v2 endpoint.
"""
if not token:
print("Token validation failed: Token is empty.")
return False
try:
headers = {"Authorization": f"Bearer {token}"}
response = requests.get("https://huggingface.co/api/whoami-v2", headers=headers, timeout=10)
if response.status_code == 200:
user_info = response.json()
username = user_info.get('name', 'Unknown')
print(f"Token validation successful. Authenticated as: {username}")
return True
else:
print(f"Token validation failed: API returned status code {response.status_code}. Response: {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"Token validation failed: Network error occurred: {e}")
return False
except Exception as e:
print(f"Token validation failed: Unexpected error: {e}")
return False
def ask_question(question, hf_token, model_id):
global index, chunks
print("--- Starting ask_question function ---")
# 1. Check index
print(f"1. Checking index and chunks...")
if index is None:
error_msg = "Index not built. Please check the application startup logs for indexing errors."
print(f" -> {error_msg}")
return error_msg
if len(chunks) == 0:
error_msg = "Index is empty (no chunks found). Please check document loading and crawling."
print(f" -> {error_msg}")
return error_msg
print(f" -> Index and chunks OK. Number of chunks: {len(chunks)}")
try:
# 2. Validate Token
print(f"2. Validating Hugging Face token...")
if not is_token_valid(hf_token):
error_msg = "Invalid or missing Hugging Face token. Please check your token."
print(f" -> {error_msg}")
return error_msg
print(" -> Token validated successfully.")
# 3. Check Model Accessibility
print(f"3. Checking model accessibility: '{model_id}'...")
if not is_model_accessible(hf_token, model_id):
error_msg = f"Model '{model_id}' is not accessible with the provided token or does not exist. Please check the model ID and token permissions."
print(f" -> {error_msg}")
return error_msg
print(" -> Model accessibility check passed.")
# 4. Process Question
print(f"4. Processing question: '{question}' using model '{model_id}'...")
# --- Detailed Steps with Logging ---
print(" a. Encoding question...")
q_emb = embedder.encode([question], convert_to_numpy=True)
print(f" -> Encoding successful. Shape: {q_emb.shape}, Type: {q_emb.dtype}")
print(" b. Ensuring float32 dtype...")
q_emb = q_emb.astype('float32')
print(f" -> Dtype conversion successful. New Type: {q_emb.dtype}")
print(" c. Searching FAISS index for relevant chunks...")
D, I = index.search(q_emb, k=3)
print(f" -> Search successful. Distances: {D}, Indices: {I}")
print(" d. Retrieving text chunks...")
retrieved = [chunks[i] for i in I[0] if i < len(chunks)]
print(f" -> Retrieved {len(retrieved)} chunks.")
print(" e. Building context string...")
context = "\n".join(retrieved)
print(f" -> Context built. Length: {len(context)} characters.")
print(" f. Constructing prompt...")
prompt = f"Answer based on context:\n{context}\nQuestion: {question}\nAnswer:"
print(f" -> Prompt constructed. Length: {len(prompt)} characters.")
# 5. Call Model API
print("5. Initializing InferenceClient...")
client = InferenceClient(model=model_id, token=hf_token)
print(" -> InferenceClient initialized.")
print("6. Calling model API (text_generation)...")
# --- Potential Workaround ---
# Instead of letting the client auto-detect, explicitly specify the endpoint URL
# This bypasses some of the internal routing logic that might be failing.
try:
# Try the standard call first
resp = client.text_generation(prompt, max_new_tokens=200, temperature=0.7)
print(" -> Model API call (standard) successful.")
except StopIteration:
# If the specific error occurs, try the workaround
print(" -> Standard call failed with StopIteration. Trying direct endpoint URL...")
# Construct the direct Inference API endpoint URL for the model
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
headers = {"Authorization": f"Bearer {hf_token}"}
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 200,
"temperature": 0.7,
# Add other parameters if needed
}
}
print(f" -> Sending POST request to {api_url}")
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
# Handle the response based on the model type
# For text2text-generation models like Flan-T5, the response is usually JSON with a 'generated_text' key
# For text-generation models, it might be just the text or a list
# Let's try parsing as JSON first
try:
result = response.json()
if isinstance(result, list) and len(result) > 0:
# Often the first item is a dict with 'generated_text'
if isinstance(result[0], dict) and 'generated_text' in result[0]:
resp = result[0]['generated_text']
else:
# If it's a list of strings or other format, adapt here
resp = str(result[0])
elif isinstance(result, dict) and 'generated_text' in result:
resp = result['generated_text']
else:
# Fallback: convert the whole response to string
resp = str(result)
except requests.exceptions.JSONDecodeError:
# If not JSON, assume raw text (less common for this endpoint but possible)
resp = response.text
print(" -> Model API call (direct) successful.")
print("--- ask_question function completed successfully ---")
return resp
except requests.exceptions.RequestException as e: # Catch network-related errors from requests
error_msg = f"Network error calling model API: {e}"
print(f" -> {error_msg}")
return error_msg
except Exception as e:
error_msg = f"Error processing question: {str(e)}"
print(f" -> {error_msg}")
import traceback
print(" -> Full Traceback:")
traceback.print_exc()
print("--- ask_question function failed ---")
return error_msg # Return the error message to the UI
# ... (rest of the code remains the same)
# ==========================
# Build index on startup
# ==========================
build_index() # This will run when the script is loaded
# ==========================
# API Endpoint for Hugging Face
# ==========================
def predict_api(question: str, hf_token: str, model_id: str):
"""API endpoint for Hugging Face Spaces - returns JSON format"""
answer = ask_question(question, hf_token, model_id)
return {"data": [answer]}
def ui_ask_question(question: str, hf_token: str, model_id: str):
"""Function for the UI button - returns plain text"""
if not question.strip():
return "Please enter a question."
if not hf_token.strip():
return "Please enter your Hugging Face token."
answer = ask_question(question, hf_token, model_id)
return answer
# ==========================
# UI
# ==========================
with gr.Blocks(title="UCC RAG Chatbot") as demo:
gr.Markdown("# 📚 UCC RAG Chatbot")
gr.Markdown("Ask questions about the Uganda Communications Commission (UCC) using its public information.")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Configuration")
hf_token = gr.Textbox(label="Hugging Face Token", type="password", info="Get your token from https://huggingface.co/settings/tokens")
model_id = gr.Textbox(label="Model ID", value="deepseek-llm-7b-base", info="ID of the Hugging Face model to use")
with gr.Column(scale=2):
gr.Markdown("### Ask a Question")
question = gr.Textbox(label="Your Question", placeholder="e.g., What is the role of UCC?")
answer = gr.Textbox(label="Answer", interactive=False, lines=5)
ask_btn = gr.Button("Ask", variant="primary")
# Use the UI function for the button
ask_btn.click(ui_ask_question, inputs=[question, hf_token, model_id], outputs=answer)
# ==========================
# API Interface (Optional, kept for API access)
# ==========================
# api_interface = gr.Interface(
# fn=predict_api,
# inputs=[
# gr.Textbox(label="Question"),
# gr.Textbox(label="HF Token", type="password"),
# gr.Textbox(label="Model ID", value="deepseek-llm-7b-base")
# ],
# outputs=gr.JSON(),
# title="API Endpoint",
# description="Use this endpoint for API calls via Postman or scripts.",
# api_name="predict_api" # This makes it available at /api/predict_api
# )
# ==========================
# Launch the main UI
# ==========================
if __name__ == "__main__":
# If you want to run the API interface as well, you can combine Blocks and Interface
# But typically, you'd use Blocks for complex UIs.
# Let's launch just the Blocks UI for now.
demo.launch(server_name="0.0.0.0", server_port=7860)