tejoess commited on
Commit
c414a2a
Β·
verified Β·
1 Parent(s): 7839f4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -57
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 langchain_community.llms import LlamaCpp # To run the GGUF model
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 document extractor",
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 hardcoded URS document context using Llama 3 on Hugging Face Spaces.")
21
 
22
- # --- Core Logic from your script ---
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
- # Use Streamlit's cache to load the model only once
 
47
  @st.cache_resource
48
  def get_llm():
49
- """Loads the Llama 3 model from Hugging Face."""
50
- st.info("Downloading and loading the Llama 3 model... This may take a few minutes on first run.")
51
-
52
- # This function will download the model from the Hugging Face Hub and cache it.
53
- llm = LlamaCpp(
54
- # The repo ID of the model you want to use
55
- model_path_or_repo_id="QuantFactory/Meta-Llama-3-8B-Instruct-GGUF",
56
- # The specific model file in that repository
57
- model_basename="Meta-Llama-3-8B-Instruct.Q4_K_M.gguf",
58
- n_gpu_layers=-1, # Offload all layers to GPU if available (won't use on basic HF space)
59
- n_batch=512,
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.info("Click the button below to start the extraction process. Note: This uses a large model on a free CPU, so it will be slow and may take several minutes.")
108
-
109
- if st.button("✨ Start Extraction", type="primary"):
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
- # The model is instructed to return a JSON block. Find it.
127
- json_start = raw_response.find('{')
128
- json_end = raw_response.rfind('}') + 1
129
- json_str = raw_response[json_start:json_end]
130
- result = json.loads(json_str)
131
 
132
- st.success("βœ… Extraction Complete!")
133
- st.subheader("Parsed JSON Result")
134
- st.json(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  except Exception as e:
137
- st.error(f"⚠️ Failed to parse JSON from the model's response. Error: {e}")
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}")