CoolFace
Apppublic

dindizz/SubsurfaceSentinel

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py65 linesDownload Raw Back to root
1import openai2import gradio as gr3import os4import time5 6# Load your OpenAI API key from the environment variable7api_key = os.getenv("OPENAI_API_KEY")8 9# Ensure the API key doesn't have any leading/trailing whitespace or newline characters10openai.api_key = api_key.strip()11 12# Function to interact with the GPT-4o-mini model via the OpenAI API13def subsurface_sentinel(user_input):14    prompt = f"User: {user_input}"15 16    # Retry mechanism in case of connection errors17    for _ in range(3):  # Try up to 3 times18        try:19            response = openai.chat.completions.create(20                model="gpt-4o-mini",  # Use the gpt-4o-mini model21                messages=[22                    {"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."},23                    {"role": "user", "content": prompt}24                ],25                max_tokens=150,26                temperature=0.7,27            )28            # Extract the response text and remove any leading/trailing whitespace29            return response.choices[0].message.content.strip()30        except openai.error.APIConnectionError as e:31            print(f"Connection error: {e}. Retrying...")32            time.sleep(2)  # Wait for 2 seconds before retrying33        except openai.InvalidRequestError as e:34            return f"Invalid request: {e}"35        except openai.AuthenticationError as e:36            return f"Authentication failed: {e}"37        except openai.RateLimitError as e:38            return f"Rate limit exceeded: {e}"39        except Exception as e:40            return f"An unexpected error occurred: {e}"41    42    return "Failed to connect to OpenAI API after several attempts."43 44# Create the Gradio interface45iface = gr.Interface(46    fn=subsurface_sentinel,  # The function to be called for generating responses47    inputs=gr.Textbox(label="Enter your question here"),  # User input48    outputs="text",  # Output type for the bot's response49    title="Subsurface Sentinel",50    description=(51        "A virtual assistant for professionals in the oil and gas industry focusing on field development planning.\n\n"52        "You can ask questions related to field development, subsurface risks, reservoir management, and more."53    ),54    examples=[55        ["Analyze the provided Field Development Plan for insights."],56        ["What are the potential risks in subsurface operations?"],57        ["How can machine learning be applied to improve reservoir management?"],58        ["Summarize the key details from the latest geological survey report."],59        ["Evaluate the proposed drilling strategy in terms of cost-effectiveness."]60    ]61)62 63# Launch the Gradio interface64iface.launch()65