Kathirsci commited on
Commit
a8c600f
·
verified ·
1 Parent(s): ffb4b75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -19,7 +19,8 @@ logger = logging.getLogger(__name__)
19
 
20
  # Constants
21
  EMBEDDING_MODEL = 'sentence-transformers/all-MiniLM-L6-v2'
22
- DEFAULT_MODEL = "distilgpt2" # A smaller model that's more likely to work in Spaces
 
23
 
24
  # Check for GPU
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -36,12 +37,12 @@ def load_embeddings():
36
  return None
37
 
38
  @st.cache_resource
39
- def load_llm(model_name):
40
  """Load and cache the language model."""
41
  try:
42
  tokenizer = AutoTokenizer.from_pretrained(model_name)
43
  model = AutoModelForCausalLM.from_pretrained(model_name)
44
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device, max_length=512)
45
  return HuggingFacePipeline(pipeline=pipe)
46
  except Exception as e:
47
  logger.error(f"Failed to load LLM: {e}")
@@ -78,7 +79,7 @@ def summarize_report(documents: List[Document], llm) -> str:
78
  """Summarize the report using the loaded model."""
79
  try:
80
  prompt_template = """
81
- Summarize the following text in a clear and concise manner:
82
 
83
  {text}
84
 
@@ -99,10 +100,11 @@ def main():
99
  st.title("Report Summarizer")
100
 
101
  model_option = st.sidebar.text_input("Enter model name", value=DEFAULT_MODEL)
 
102
 
103
  uploaded_file = st.sidebar.file_uploader("Upload your Report", type="pdf")
104
 
105
- llm = load_llm(model_option)
106
  if not llm:
107
  st.error(f"Failed to load the model {model_option}. Please try another model.")
108
  return
 
19
 
20
  # Constants
21
  EMBEDDING_MODEL = 'sentence-transformers/all-MiniLM-L6-v2'
22
+ DEFAULT_MODEL = "distilgpt2"
23
+ DEFAULT_MAX_LENGTH = 1024 # Increased default max length
24
 
25
  # Check for GPU
26
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
37
  return None
38
 
39
  @st.cache_resource
40
+ def load_llm(model_name, max_length):
41
  """Load and cache the language model."""
42
  try:
43
  tokenizer = AutoTokenizer.from_pretrained(model_name)
44
  model = AutoModelForCausalLM.from_pretrained(model_name)
45
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device, max_length=max_length)
46
  return HuggingFacePipeline(pipeline=pipe)
47
  except Exception as e:
48
  logger.error(f"Failed to load LLM: {e}")
 
79
  """Summarize the report using the loaded model."""
80
  try:
81
  prompt_template = """
82
+ Summarize the following text in a clear and concise manner. Focus on the main points and key details:
83
 
84
  {text}
85
 
 
100
  st.title("Report Summarizer")
101
 
102
  model_option = st.sidebar.text_input("Enter model name", value=DEFAULT_MODEL)
103
+ max_length = st.sidebar.slider("Max summary length", min_value=256, max_value=2048, value=DEFAULT_MAX_LENGTH, step=128)
104
 
105
  uploaded_file = st.sidebar.file_uploader("Upload your Report", type="pdf")
106
 
107
+ llm = load_llm(model_option, max_length)
108
  if not llm:
109
  st.error(f"Failed to load the model {model_option}. Please try another model.")
110
  return