Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,8 @@ import os
|
|
2 |
import re
|
3 |
import json
|
4 |
import requests
|
5 |
-
from typing import List, Dict
|
|
|
6 |
from googlesearch import search
|
7 |
import google.generativeai as genai
|
8 |
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
@@ -156,44 +157,123 @@ NAICS_CODE: [6-digit NAICS code]
|
|
156 |
"reasoning": f"Error analyzing company: {str(e)}"
|
157 |
}
|
158 |
|
159 |
-
def
|
160 |
-
"""
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
while True:
|
168 |
-
# Step 2: Get Company Info
|
169 |
-
company_name = input("\nEnter the company name (or 'exit' to quit): ")
|
170 |
-
if company_name.lower() == 'exit':
|
171 |
-
break
|
172 |
-
|
173 |
-
context = input("Enter a brief description of the company (or press Enter for none): ")
|
174 |
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
|
|
198 |
if __name__ == "__main__":
|
199 |
-
|
|
|
2 |
import re
|
3 |
import json
|
4 |
import requests
|
5 |
+
from typing import List, Dict, Optional, Tuple
|
6 |
+
import gradio as gr
|
7 |
from googlesearch import search
|
8 |
import google.generativeai as genai
|
9 |
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
|
|
157 |
"reasoning": f"Error analyzing company: {str(e)}"
|
158 |
}
|
159 |
|
160 |
+
def find_naics_code(company_name: str, context: str = "", api_key: Optional[str] = None) -> Dict:
|
161 |
+
"""
|
162 |
+
Core function to find NAICS code for a company that can be called from different interfaces
|
163 |
+
|
164 |
+
Args:
|
165 |
+
company_name: Name of the company
|
166 |
+
context: Brief description of the company (optional)
|
167 |
+
api_key: Google Gemini API key (if None, will try to get from environment variable)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
Returns:
|
170 |
+
Dictionary with NAICS code, reasoning, and optional research
|
171 |
+
"""
|
172 |
+
# Get API key from environment if not provided
|
173 |
+
if not api_key:
|
174 |
+
api_key = os.environ.get('GEMINI_API_KEY')
|
175 |
+
if not api_key:
|
176 |
+
return {
|
177 |
+
"error": "No API key provided. Set GEMINI_API_KEY environment variable or pass as parameter.",
|
178 |
+
"naics_code": "000000",
|
179 |
+
"reasoning": "Error: API key missing"
|
180 |
+
}
|
181 |
+
|
182 |
+
# Initialize Gemini model
|
183 |
+
try:
|
184 |
+
model = initialize_gemini(api_key)
|
185 |
+
except Exception as e:
|
186 |
+
return {
|
187 |
+
"error": f"Failed to initialize Gemini API: {str(e)}",
|
188 |
+
"naics_code": "000000",
|
189 |
+
"reasoning": f"Error: {str(e)}"
|
190 |
+
}
|
191 |
+
|
192 |
+
# Find NAICS Code Candidates via Google search
|
193 |
+
naics_candidates = google_search_naics(company_name)
|
194 |
|
195 |
+
# Get classification from Gemini
|
196 |
+
if not naics_candidates:
|
197 |
+
print("No NAICS codes found from Google search.")
|
198 |
+
result = get_naics_classification(model, company_name, context, [])
|
199 |
+
else:
|
200 |
+
print(f"Found {len(naics_candidates)} NAICS candidates: {naics_candidates}")
|
201 |
+
result = get_naics_classification(model, company_name, context, naics_candidates)
|
202 |
+
|
203 |
+
# Add metadata
|
204 |
+
result["company_name"] = company_name
|
205 |
+
result["context"] = context
|
206 |
+
result["candidates"] = naics_candidates
|
207 |
+
|
208 |
+
return result
|
209 |
|
210 |
+
# Gradio interface function
|
211 |
+
def classify_company(company_name: str, company_description: str, api_key: str = None) -> Tuple[str, str, str]:
|
212 |
+
"""Process inputs from Gradio and return formatted results"""
|
213 |
+
if not api_key:
|
214 |
+
api_key = os.environ.get('GEMINI_API_KEY')
|
215 |
+
|
216 |
+
if not company_name:
|
217 |
+
return "Error: Company name is required", "", ""
|
218 |
+
|
219 |
+
result = find_naics_code(company_name, company_description, api_key)
|
220 |
+
|
221 |
+
# Format the NAICS code output
|
222 |
+
naics_code = f"**NAICS Code: {result['naics_code']}**"
|
223 |
+
|
224 |
+
# Format the research output
|
225 |
+
research = ""
|
226 |
+
if "research" in result and result["research"]:
|
227 |
+
research = f"## Research on NAICS Codes\n\n{result['research']}"
|
228 |
+
|
229 |
+
# Format the reasoning output
|
230 |
+
reasoning = f"## Analysis\n\n{result['reasoning']}"
|
231 |
+
|
232 |
+
return naics_code, research, reasoning
|
233 |
|
234 |
+
# Create the Gradio interface
|
235 |
+
def create_gradio_interface():
|
236 |
+
with gr.Blocks(title="NAICS Code Finder") as demo:
|
237 |
+
gr.Markdown("# NAICS Code Finder")
|
238 |
+
gr.Markdown("Enter a company name and optional description to find the most appropriate NAICS code.")
|
239 |
+
|
240 |
+
with gr.Row():
|
241 |
+
with gr.Column():
|
242 |
+
company_name = gr.Textbox(label="Company Name", placeholder="Enter company name")
|
243 |
+
company_description = gr.Textbox(label="Company Description (optional)", placeholder="Brief description of the company")
|
244 |
+
api_key = gr.Textbox(
|
245 |
+
label="Gemini API Key (optional)",
|
246 |
+
placeholder="Enter your API key or set GEMINI_API_KEY env variable",
|
247 |
+
visible=not bool(os.environ.get('GEMINI_API_KEY'))
|
248 |
+
)
|
249 |
+
submit_btn = gr.Button("Find NAICS Code")
|
250 |
|
251 |
+
with gr.Column():
|
252 |
+
naics_output = gr.Markdown(label="NAICS Code")
|
253 |
+
research_output = gr.Markdown(label="Research")
|
254 |
+
reasoning_output = gr.Markdown(label="Reasoning")
|
255 |
+
|
256 |
+
submit_btn.click(
|
257 |
+
classify_company,
|
258 |
+
inputs=[company_name, company_description, api_key],
|
259 |
+
outputs=[naics_output, research_output, reasoning_output]
|
260 |
+
)
|
261 |
+
|
262 |
+
gr.Examples(
|
263 |
+
[
|
264 |
+
["Apple Inc", "Tech company that makes iPhones and computers"],
|
265 |
+
["Starbucks", "Coffee shop chain"],
|
266 |
+
["Bank of America", "Banking and financial services"],
|
267 |
+
["Tesla", "Electric vehicle manufacturer"]
|
268 |
+
],
|
269 |
+
inputs=[company_name, company_description]
|
270 |
+
)
|
271 |
+
|
272 |
+
return demo
|
273 |
+
|
274 |
+
# Create and launch the interface
|
275 |
+
demo = create_gradio_interface()
|
276 |
|
277 |
+
# For Spaces deployment
|
278 |
if __name__ == "__main__":
|
279 |
+
demo.launch()
|