DrishtiSharma
commited on
Create interim.py
Browse files- interim.py +84 -0
interim.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datetime import datetime
|
3 |
+
import streamlit as st
|
4 |
+
from patentwiz import preprocess_data, qa_agent
|
5 |
+
from patentwiz.main import PROMPT
|
6 |
+
|
7 |
+
# Check if the API key is loaded
|
8 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
if not api_key:
|
10 |
+
st.error("OPENAI_API_KEY not found! Please set it in the environment variables or Hugging Face Secrets.")
|
11 |
+
st.stop()
|
12 |
+
|
13 |
+
# Title and description
|
14 |
+
st.title("Technical Measurements Extractor for Patents")
|
15 |
+
st.write(
|
16 |
+
"Analyze patents to extract physical measurements such as length, mass, time, and more. "
|
17 |
+
"Provide a date to download patents, and analyze them using GPT models."
|
18 |
+
)
|
19 |
+
|
20 |
+
# User Input Section
|
21 |
+
st.header("Enter Details for Patent Analysis")
|
22 |
+
user_date_input = st.text_input("Enter a date in the format 'YYYY-MM-DD':", help="e.g., 2024-01-01")
|
23 |
+
|
24 |
+
num_patents_to_analyze = st.number_input(
|
25 |
+
"Number of patents to analyze:", min_value=1, value=1, step=1, help="Specify how many patents you want to analyze."
|
26 |
+
)
|
27 |
+
|
28 |
+
model_choice = st.selectbox(
|
29 |
+
"Select a model for analysis:", ["gpt-3.5-turbo", "gpt-4"], help="Choose the OpenAI GPT model for the analysis."
|
30 |
+
)
|
31 |
+
|
32 |
+
logging_enabled = st.checkbox("Enable logging?", value=False, help="Toggle logging for debugging purposes.")
|
33 |
+
|
34 |
+
# Run Analysis Button
|
35 |
+
if st.button("Analyze Patents"):
|
36 |
+
if not user_date_input:
|
37 |
+
st.error("Please enter a valid date!")
|
38 |
+
else:
|
39 |
+
try:
|
40 |
+
# Parse date input
|
41 |
+
input_date = datetime.strptime(user_date_input, "%Y-%m-%d")
|
42 |
+
year, month, day = input_date.year, input_date.month, input_date.day
|
43 |
+
|
44 |
+
# Step 1: Download and preprocess patents
|
45 |
+
with st.spinner("Downloading and extracting patents..."):
|
46 |
+
saved_patent_names = preprocess_data.parse_and_save_patents(
|
47 |
+
year, month, day, logging_enabled
|
48 |
+
)
|
49 |
+
if not saved_patent_names:
|
50 |
+
st.error("No patents found for the given date.")
|
51 |
+
st.stop()
|
52 |
+
st.success(f"{len(saved_patent_names)} patents found and processed!")
|
53 |
+
|
54 |
+
# Step 2: Analyze patents using GPT
|
55 |
+
random_patents = saved_patent_names[:num_patents_to_analyze]
|
56 |
+
total_cost = 0
|
57 |
+
results = []
|
58 |
+
|
59 |
+
st.write("Starting patent analysis...")
|
60 |
+
for i, patent_file in enumerate(random_patents):
|
61 |
+
cost, output = qa_agent.call_QA_to_json(
|
62 |
+
PROMPT,
|
63 |
+
year,
|
64 |
+
month,
|
65 |
+
day,
|
66 |
+
saved_patent_names,
|
67 |
+
i,
|
68 |
+
logging_enabled,
|
69 |
+
model_choice,
|
70 |
+
)
|
71 |
+
total_cost += cost
|
72 |
+
results.append(output)
|
73 |
+
|
74 |
+
# Step 3: Display results
|
75 |
+
st.write(f"**Total Cost:** ${total_cost:.4f}")
|
76 |
+
st.write("### Analysis Results:")
|
77 |
+
for idx, result in enumerate(results):
|
78 |
+
st.subheader(f"Patent {idx + 1}")
|
79 |
+
st.json(result)
|
80 |
+
|
81 |
+
except ValueError as ve:
|
82 |
+
st.error(f"Invalid date format: {ve}")
|
83 |
+
except Exception as e:
|
84 |
+
st.error(f"An unexpected error occurred: {e}")
|