|
import streamlit as st |
|
import gradio as gr |
|
import numpy as np |
|
from audiorecorder import audiorecorder |
|
import whisper |
|
import os |
|
import streamlit.components.v1 as components |
|
import tempfile |
|
import io |
|
import requests |
|
import json |
|
|
|
def chunk_text(text, chunk_size=2000): |
|
chunks = [] |
|
start = 0 |
|
while start < len(text): |
|
end = start + chunk_size |
|
chunk = text[start:end] |
|
chunks.append(chunk) |
|
start = end |
|
return chunks |
|
|
|
|
|
if 'learning_objectives' not in st.session_state: |
|
st.session_state.learning_objectives = "" |
|
|
|
|
|
st.title("Patent Claims Extraction") |
|
|
|
|
|
api_key = st.text_input("Enter your OpenAI API Key:", type="password") |
|
|
|
|
|
image = st.camera_input("Camera input") |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tf: |
|
if image: |
|
tf.write(image.read()) |
|
temp_image_path = tf.name |
|
else: |
|
temp_image_path = None |
|
|
|
|
|
audio = st.audio_recorder("Click to record audio", "Click to stop recording") |
|
|
|
submit_button = st.button("Use this audio") |
|
|
|
if submit_button: |
|
model = whisper.load_model("base") |
|
result = model.transcribe(audio) |
|
st.info("Transcribing...") |
|
st.success("Transcription complete") |
|
transcript = result['text'] |
|
|
|
with st.expander("See transcript"): |
|
st.markdown(transcript) |
|
|
|
|
|
model_choice = st.selectbox( |
|
"Select the model you want to use:", |
|
["gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "gpt-3.5-turbo", "gpt-4-0314", "gpt-4-0613", "gpt-4"] |
|
) |
|
|
|
|
|
context = "You are a patent claims identifier and extractor. You will freeform text, identify any claims contained therein that may be patentable. You identify, extract, print such claims, briefly explain why each claim is patentable." |
|
userinput = st.text_input("Input Text:", "Freeform text here!") |
|
|
|
|
|
if api_key: |
|
openai.api_key = api_key |
|
|
|
|
|
st.write("### Patentable Claims:") |
|
|
|
claims_extraction = "" |
|
|
|
learning_status_placeholder = st.empty() |
|
disable_button_bool = False |
|
|
|
if userinput and api_key and st.button("Extract Claims", key="claims_extraction", disabled=disable_button_bool): |
|
|
|
input_chunks = chunk_text(userinput) |
|
|
|
|
|
all_extracted_claims = "" |
|
|
|
for chunk in input_chunks: |
|
|
|
learning_status_placeholder.text(f"Extracting Patentable Claims for chunk {input_chunks.index(chunk) + 1}...") |
|
|
|
|
|
claims_extraction_response = openai.ChatCompletion.create( |
|
model=model_choice, |
|
messages=[ |
|
{"role": "user", "content": f"Extract any patentable claims from the following: \n {chunk}. \n Extract each claim. Briefly explain why you extracted this word phrase. Exclude any additional commentary."} |
|
] |
|
) |
|
|
|
|
|
claims_extraction = claims_extraction_response['choices'][0]['message']['content'] |
|
|
|
|
|
all_extracted_claims += claims_extraction.strip() |
|
|
|
|
|
st.session_state.claims_extraction = all_extracted_claims |
|
|
|
|
|
learning_status_placeholder.text(f"Patentable Claims Extracted!\n{all_extracted_claims.strip()}") |
|
|
|
|
|
if st.button("Extract Claims") and api_key and transcript: |
|
|
|
|
|
|
|
|
|
input_chunks = chunk_text(transcript) |
|
|
|
|
|
all_extracted_claims = "" |
|
|
|
for chunk in input_chunks: |
|
|
|
learning_status_placeholder.text(f"Extracting Patentable Claims for chunk {input_chunks.index(chunk) + 1}...") |
|
|
|
|
|
claims_extraction_response = openai.ChatCompletion.create( |
|
model=model_choice, |
|
messages=[ |
|
{"role": "user", "content": f"Extract any patentable claims from the following: \n {chunk}. \n Extract each claim. Briefly explain why you extracted this word phrase. Exclude any additional commentary."} |
|
] |
|
) |
|
|
|
|
|
claims_extraction = claims_extraction_response['choices'][0]['message']['content'] |
|
|
|
|
|
all_extracted_claims += claims_extraction.strip() |
|
|
|
|
|
st.session_state.claims_extraction = all_extracted_claims |
|
|
|
|
|
learning_status_placeholder.text(f"Patentable Claims Extracted!\n{all_extracted_claims.strip()}") |
|
|
|
|
|
lesson_plan = st.text("Extracting Patentable Claims...") |
|
|
|
|
|
assistant_reply = claims_extraction_response['choices'][0]['message']['content'] |
|
claims_extraction = st.text(assistant_reply.strip()) |
|
|
|
|
|
st.markdown("<sub>This app was created by [Taylor Ennen](https://github.com/taylor-ennen/GPT-Streamlit-MVP) & [Tonic](https://huggingface.co/tonic)</sub>", unsafe_allow_html=True) |
|
|