NavyDevilDoc commited on
Commit
fd7e8c4
·
verified ·
1 Parent(s): 808a777

Update app.py

Browse files

added feature to query an LLM about the top selection

Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -6,6 +6,7 @@ import numpy as np
6
  from sentence_transformers import SentenceTransformer, CrossEncoder
7
  from huggingface_hub import HfApi, hf_hub_download
8
  from huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError
 
9
  import pypdf
10
  import docx
11
  import time
@@ -91,6 +92,38 @@ def recursive_chunking(text, source, chunk_size=500, overlap=100):
91
  chunks.append({"text": chunk_text, "source": source})
92
  return chunks
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  # --- CORE SEARCH ENGINE ---
95
  class DocSearchEngine:
96
  def __init__(self):
 
6
  from sentence_transformers import SentenceTransformer, CrossEncoder
7
  from huggingface_hub import HfApi, hf_hub_download
8
  from huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError
9
+ from huggingface_hub import InferenceClient
10
  import pypdf
11
  import docx
12
  import time
 
92
  chunks.append({"text": chunk_text, "source": source})
93
  return chunks
94
 
95
+ def ask_llm(query, context):
96
+ """
97
+ Sends the user query and the retrieved document text to a free, hosted LLM.
98
+ """
99
+ if not HF_TOKEN:
100
+ return "Error: HF_TOKEN is missing. Cannot contact AI."
101
+
102
+ # We use Mistral-7B-Instruct because it is fast, follows instructions well,
103
+ # and is usually available on the free tier.
104
+ repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
105
+
106
+ client = InferenceClient(model=repo_id, token=HF_TOKEN)
107
+
108
+ prompt = f"""
109
+ You are a Senior Navy Yeoman and Subject Matter Expert.
110
+ Analyze the following Navy document excerpt and answer the user's question based ONLY on that text.
111
+
112
+ USER QUESTION: "{query}"
113
+
114
+ DOCUMENT EXCERPT:
115
+ "{context}"
116
+
117
+ Your Answer (Be concise, professional, and cite the document):
118
+ """
119
+
120
+ try:
121
+ # stream=True makes it look cool (typewriter effect) but standard return is easier for now
122
+ response = client.text_generation(prompt, max_new_tokens=400)
123
+ return response
124
+ except Exception as e:
125
+ return f"AI Error: {e}"
126
+
127
  # --- CORE SEARCH ENGINE ---
128
  class DocSearchEngine:
129
  def __init__(self):