Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,20 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.documents import Document
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
-
from
|
| 6 |
-
from langchain_community.vectorstores import FAISS
|
| 7 |
-
from langchain.text_splitter import MarkdownHeaderTextSplitter
|
| 8 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 9 |
import json
|
| 10 |
-
import hashlib
|
| 11 |
|
| 12 |
# --- Streamlit Page Configuration ---
|
| 13 |
st.set_page_config(
|
| 14 |
-
page_title="URS
|
| 15 |
page_icon="π",
|
| 16 |
layout="wide"
|
| 17 |
)
|
| 18 |
|
| 19 |
-
st.title("π URS Document Data Extractor")
|
| 20 |
-
st.markdown("This app extracts key information from a
|
| 21 |
|
| 22 |
-
# --- Core Logic
|
| 23 |
|
| 24 |
# Using the hardcoded context you provided
|
| 25 |
CONTEXT_DOCUMENTS = [
|
|
@@ -43,25 +39,21 @@ FINAL_QUERY = {
|
|
| 43 |
"[Requirement number_MOC]": "Which section number corresponds to the slope value of the desired level of instruments and accuracy levels?"
|
| 44 |
}
|
| 45 |
|
| 46 |
-
#
|
|
|
|
| 47 |
@st.cache_resource
|
| 48 |
def get_llm():
|
| 49 |
-
"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
n_ctx=4096, # Context window
|
| 61 |
-
f16_kv=True, # Must be set to True on metal for Apple silicon
|
| 62 |
-
verbose=True,
|
| 63 |
-
temperature=0,
|
| 64 |
-
max_tokens=2048
|
| 65 |
)
|
| 66 |
return llm
|
| 67 |
|
|
@@ -103,39 +95,41 @@ def generating_results(context, Final_Query, llm):
|
|
| 103 |
return response
|
| 104 |
|
| 105 |
# --- Streamlit App UI ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
st.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
with st.spinner("Please wait... The Llama 3 model is processing the document..."):
|
| 111 |
-
try:
|
| 112 |
-
# 1. Load the LLM
|
| 113 |
-
# This is cached, so it's fast after the first run.
|
| 114 |
-
llm_extraction = get_llm()
|
| 115 |
-
|
| 116 |
-
# 2. Call the generation function
|
| 117 |
-
# The context is already hardcoded in this example.
|
| 118 |
-
# In a real app, you would generate it from a file upload.
|
| 119 |
-
raw_response = generating_results(CONTEXT_DOCUMENTS, FINAL_QUERY, llm_extraction)
|
| 120 |
-
|
| 121 |
-
st.subheader("Raw Model Output")
|
| 122 |
-
st.text(raw_response)
|
| 123 |
-
|
| 124 |
-
# 3. Safely parse the JSON from the response
|
| 125 |
try:
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
json_end = raw_response.rfind('}') + 1
|
| 129 |
-
json_str = raw_response[json_start:json_end]
|
| 130 |
-
result = json.loads(json_str)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
-
st.error(f"
|
| 138 |
-
st.text_area("Model's Raw Response for Debugging:", raw_response, height=300)
|
| 139 |
-
|
| 140 |
-
except Exception as e:
|
| 141 |
-
st.error(f"An unexpected error occurred during the extraction process: {e}")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.documents import Document
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# --- Streamlit Page Configuration ---
|
| 9 |
st.set_page_config(
|
| 10 |
+
page_title="URS Document Extractor",
|
| 11 |
page_icon="π",
|
| 12 |
layout="wide"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
st.title("π URS Document Data Extractor (API Version)")
|
| 16 |
+
st.markdown("This app extracts key information from a URS document using the fast and reliable Hugging Face Inference API.")
|
| 17 |
|
| 18 |
+
# --- Core Logic ---
|
| 19 |
|
| 20 |
# Using the hardcoded context you provided
|
| 21 |
CONTEXT_DOCUMENTS = [
|
|
|
|
| 39 |
"[Requirement number_MOC]": "Which section number corresponds to the slope value of the desired level of instruments and accuracy levels?"
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# --- Function to initialize the LLM ---
|
| 43 |
+
# This uses the Hugging Face Inference API. It's fast and reliable.
|
| 44 |
@st.cache_resource
|
| 45 |
def get_llm():
|
| 46 |
+
"""Initializes the HuggingFaceEndpoint for the Llama 3 model."""
|
| 47 |
+
# Ensure the secret is available before initializing
|
| 48 |
+
if "HF_TOKEN" not in st.secrets:
|
| 49 |
+
st.error("Hugging Face token not found! Please add it to your Space secrets.", icon="π¨")
|
| 50 |
+
st.stop()
|
| 51 |
+
|
| 52 |
+
llm = HuggingFaceEndpoint(
|
| 53 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 54 |
+
huggingfacehub_api_token=st.secrets["HF_TOKEN"],
|
| 55 |
+
temperature=0.1,
|
| 56 |
+
max_new_tokens=2048,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
return llm
|
| 59 |
|
|
|
|
| 95 |
return response
|
| 96 |
|
| 97 |
# --- Streamlit App UI ---
|
| 98 |
+
if 'show_button' not in st.session_state:
|
| 99 |
+
st.session_state.show_button = True
|
| 100 |
+
|
| 101 |
+
# We need the secret to run, so we check for it first.
|
| 102 |
+
if "HF_TOKEN" not in st.secrets:
|
| 103 |
+
st.error("A Hugging Face Token is required to run this app. Please add it to your Space secrets in the 'Settings' tab.", icon="π")
|
| 104 |
+
st.session_state.show_button = False
|
| 105 |
|
| 106 |
+
if st.session_state.show_button:
|
| 107 |
+
if st.button("β¨ Start Extraction", type="primary"):
|
| 108 |
+
with st.spinner("Calling the Llama 3 Inference API... This should be fast!"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
+
# 1. Load the LLM via the API
|
| 111 |
+
llm_extraction = get_llm()
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# 2. Call the generation function
|
| 114 |
+
raw_response = generating_results(CONTEXT_DOCUMENTS, FINAL_QUERY, llm_extraction)
|
| 115 |
+
|
| 116 |
+
st.subheader("Raw Model Output")
|
| 117 |
+
st.text(raw_response)
|
| 118 |
+
|
| 119 |
+
# 3. Safely parse the JSON from the response
|
| 120 |
+
try:
|
| 121 |
+
json_start = raw_response.find('{')
|
| 122 |
+
json_end = raw_response.rfind('}') + 1
|
| 123 |
+
json_str = raw_response[json_start:json_end]
|
| 124 |
+
result = json.loads(json_str)
|
| 125 |
+
|
| 126 |
+
st.success("β
Extraction Complete!")
|
| 127 |
+
st.subheader("Parsed JSON Result")
|
| 128 |
+
st.json(result)
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(f"β οΈ Failed to parse JSON from the model's response. Error: {e}")
|
| 132 |
+
st.text_area("Model's Raw Response for Debugging:", raw_response, height=300)
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
+
st.error(f"An unexpected error occurred during the extraction process: {e}")
|
|
|
|
|
|
|
|
|
|
|
|