Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,51 @@
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
|
| 6 |
# Load your OpenAI API key from the environment variable
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
-
# Function to extract text from an uploaded PDF
|
| 10 |
-
def extract_text_from_pdf(pdf_file):
|
| 11 |
-
reader = PyPDF2.PdfReader(pdf_file)
|
| 12 |
-
text = ""
|
| 13 |
-
for page in reader.pages:
|
| 14 |
-
text += page.extract_text()
|
| 15 |
-
return text
|
| 16 |
-
|
| 17 |
# Function to interact with the GPT-4o-mini model via the OpenAI API
|
| 18 |
-
def subsurface_sentinel(user_input
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if pdf_file is not None:
|
| 22 |
-
context = extract_text_from_pdf(pdf_file)
|
| 23 |
-
# Truncate or summarize context if needed, depending on the length
|
| 24 |
-
context = context[:2000] # Truncate to first 2000 characters for example
|
| 25 |
-
|
| 26 |
-
# Combine the context with the user input
|
| 27 |
-
prompt = f"Context: {context}\n\nUser: {user_input}"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
return response.choices[0].message.content.strip()
|
| 41 |
|
| 42 |
# Create the Gradio interface
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=subsurface_sentinel, # The function to be called for generating responses
|
| 45 |
-
inputs=
|
| 46 |
-
gr.Textbox(label="Enter your question here"), # User input
|
| 47 |
-
gr.File(label="Upload a PDF for context (optional)") # PDF upload input
|
| 48 |
-
],
|
| 49 |
outputs="text", # Output type for the bot's response
|
| 50 |
title="Subsurface Sentinel",
|
| 51 |
description=(
|
| 52 |
"A virtual assistant for professionals in the oil and gas industry focusing on field development planning.\n\n"
|
| 53 |
-
"You can ask
|
| 54 |
),
|
| 55 |
examples=[
|
| 56 |
-
["Analyze the provided Field Development Plan for insights."
|
| 57 |
-
["What are the potential risks in subsurface operations?",
|
|
|
|
|
|
|
|
|
|
| 58 |
]
|
| 59 |
)
|
| 60 |
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
# Load your OpenAI API key from the environment variable
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Function to interact with the GPT-4o-mini model via the OpenAI API
|
| 10 |
+
def subsurface_sentinel(user_input):
|
| 11 |
+
prompt = f"User: {user_input}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Retry mechanism in case of connection errors
|
| 14 |
+
for _ in range(3): # Try up to 3 times
|
| 15 |
+
try:
|
| 16 |
+
response = openai.chat.completions.create(
|
| 17 |
+
model="gpt-4o-mini", # Use the gpt-4o-mini model
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
],
|
| 22 |
+
max_tokens=150,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
)
|
| 25 |
+
# Extract the response text and remove any leading/trailing whitespace
|
| 26 |
+
return response.choices[0].message.content.strip()
|
| 27 |
+
except openai.error.APIConnectionError as e:
|
| 28 |
+
print(f"Connection error: {e}. Retrying...")
|
| 29 |
+
time.sleep(2) # Wait for 2 seconds before retrying
|
| 30 |
|
| 31 |
+
return "Failed to connect to OpenAI API after several attempts."
|
|
|
|
| 32 |
|
| 33 |
# Create the Gradio interface
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=subsurface_sentinel, # The function to be called for generating responses
|
| 36 |
+
inputs=gr.Textbox(label="Enter your question here"), # User input
|
|
|
|
|
|
|
|
|
|
| 37 |
outputs="text", # Output type for the bot's response
|
| 38 |
title="Subsurface Sentinel",
|
| 39 |
description=(
|
| 40 |
"A virtual assistant for professionals in the oil and gas industry focusing on field development planning.\n\n"
|
| 41 |
+
"You can ask questions related to field development, subsurface risks, reservoir management, and more."
|
| 42 |
),
|
| 43 |
examples=[
|
| 44 |
+
["Analyze the provided Field Development Plan for insights."],
|
| 45 |
+
["What are the potential risks in subsurface operations?"],
|
| 46 |
+
["How can machine learning be applied to improve reservoir management?"],
|
| 47 |
+
["Summarize the key details from the latest geological survey report."],
|
| 48 |
+
["Evaluate the proposed drilling strategy in terms of cost-effectiveness."]
|
| 49 |
]
|
| 50 |
)
|
| 51 |
|