SagaciousIP/Drafting
0
1import gradio as gr2import openai3import os4import requests5import replicate6from docx import Document7from gradio.themes.utils import colors8 9 10# Define the theme11class BlueTheme(gr.themes.Base):12 13 def __init__(self):14 super().__init__(primary_hue=colors.blue,15 secondary_hue=colors.sky,16 neutral_hue=colors.gray,17 font=[gr.themes.GoogleFont("Roboto"), "sans-serif"],18 font_mono=[gr.themes.GoogleFont("Lato"), "monospace"])19 super().set(20 body_background_fill=21 "linear-gradient(90deg, *primary_200, *primary_500)",22 button_primary_background_fill="*secondary_300",23 button_primary_background_fill_hover="*secondary_400",24 button_primary_text_color="white",25 )26 27 28blue_theme = BlueTheme()29 30# Retrieve the OpenAI API key from Replit secrets31openai_api_key = os.getenv("OPENAI_API_KEY")32if not openai_api_key:33 raise ValueError(34 "Please set your OpenAI API key in Replit secrets as 'OPENAI_API_KEY'.")35 36# Set up your OpenAI API key37openai.api_key = openai_api_key38 39# Your detailed prompt goes here ...40detailed_prompt = (41 "You are a patent drafter. Your objective is to draft an entire patent. "42 "I will give you an invention disclosure (IDF) and you will generate the entire patent step by step. "43 "First, you will generate a background for the patent, writing some technology focus of the patent claim "44 "and the prior art issues addressed by the patent based on the invention disclosure. "45 "Then, you will generate a set of 8 patent claims (1 independent and 7 dependent) based on the invention disclosure and the background. Don't write words like independent and dependent in the claims themselves, also make the independent claim look like US patents where independent claims have multiple claim elements/lines"46 "Then, you will generate a detailed description of the patent, the detailed description "47 "supporting the patent claims and solving the prior art issues. "48 "They you will generate a title and an abstract for the drafted patent application. "49 "Then you will generate brief description of the drawings having figure notations for various types of patent illustrations relevant to our patent"50 "(please note that the first drawing should be a flowchart, all other can be a mix of flowchart, block diagrams, system diagrams, etc.)"51 "supporting the claimed patent functionality. "52 "An important note is to make the patent draft cohesive so that each part generated by you as a part of the patent draft "53 "should be contextually same. Also ensure that various parts of the patent are in correct order "54 "(title, then abstract, then background, then claims, then figure notations, then detailed description). "55 "Please also make sure that the patent is per USPTO requirements (similar to US patents, don't be stressed okay) "56 "and there is no extra irrelevant text (only the entire patent draft). Also make sure your entire output (entire patent) is limited to 600 words. The invention disclosure (IDF) is: "57)58 59 60# Modify the generate_word_doc function to be more flexible61def generate_word_doc(text):62 doc = Document()63 doc.add_heading('Patent Draft', 0)64 doc.add_paragraph(text)65 file_name = "patent_draft.docx"66 doc.save(file_name)67 return file_name68 69# Function to draft patent using GPT-470def draft_patent_gpt4(idf):71 try:72 response = openai.ChatCompletion.create(model="gpt-4",73 messages=[{74 "role":75 "system",76 "content":77 "You are a helpful assistant."78 }, {79 "role":80 "user",81 "content":82 detailed_prompt + idf83 }])84 85 if "error" in response:86 return f"Error from OpenAI: {response['error']}", None87 88 patent_text = response['choices'][0]['message']['content']89 file_path = generate_word_doc(patent_text)90 return patent_text, file_path91 except Exception as e:92 return f"Unexpected error occurred: {str(e)}", None93 94 95def draft_patent_llama(idf):96 try:97 # Retrieve the REPLICATE_API_TOKEN from Replit secrets98 replicate_api_token = os.getenv("REPLICATE_API_TOKEN")99 if not replicate_api_token:100 raise ValueError(101 "Please set your Replicate API token in Replit secrets as 'REPLICATE_API_TOKEN'."102 )103 104 # Set the API token as an environment variable105 os.environ["REPLICATE_API_TOKEN"] = replicate_api_token106 107 input_data = {108 "prompt":109 "You are a legal attorney. Create a patent draft comprising a title, an abstract, a background, few claims, and detailed description for this invention:"110 + idf,111 "system_prompt":112 "You are a helpful assistant.",113 # You can adjust the other parameters (max_new_tokens, temperature, etc.) if needed.114 }115 116 model_name = "replicate/llama-2-70b-chat"117 model_version = model_name + ":2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf"118 119 output = replicate.run(model_name=model_name,120 model_version=model_version,121 input=input_data)122 patent_text = "".join([item for item in output123 ]) # concatenate the output strings124 125 file_path = generate_word_doc(patent_text)126 return patent_text, file_path127 except Exception as e:128 return f"Unexpected error occurred: {str(e)}", None129 130 131gpt4_interface = gr.Interface(132 fn=draft_patent_gpt4,133 inputs=gr.components.Textbox(label="Please Enter Invention Disclosure"),134 outputs=[135 gr.components.Textbox(label="Generated Draft"),136 gr.components.File(label="Download Patent Draft")137 ],138 live=False) # Change this to False139 140llama_interface = gr.Interface(141 fn=draft_patent_llama,142 inputs=gr.components.Textbox(label="Please Enter Invention Disclosure"),143 outputs=[144 gr.components.Textbox(label="Generated Draft"),145 gr.components.File(label="Download Patent Draft")146 ],147 live=False) # Change this to False148 149# Use the theme for your Gradio interface150tabbed_interface = gr.TabbedInterface([gpt4_interface, llama_interface],151 ["Pro", "Experimental"],152 title="AI Patent Drafter",153 theme=blue_theme)154 155if __name__ == "__main__":156 tabbed_interface.launch(share=False)