Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,62 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
response =
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
+
import openai
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import PyPDF2
|
| 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, pdf_file=None):
|
| 19 |
+
context = ""
|
| 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 |
+
response = openai.chat.completions.create( # Correct method usage
|
| 30 |
+
model="gpt-4o-mini", # Use the gpt-4o-mini model
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": "system", "content": "You are Subsurface Sentinel, a virtual assistant for professionals in the oil and gas industry, focusing on field development planning."},
|
| 33 |
+
{"role": "user", "content": prompt}
|
| 34 |
+
],
|
| 35 |
+
max_tokens=150,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Extract the response text and remove any leading/trailing whitespace
|
| 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 a question or upload a PDF to provide additional context for your query."
|
| 54 |
+
),
|
| 55 |
+
examples=[
|
| 56 |
+
["Analyze the provided Field Development Plan for insights.", None],
|
| 57 |
+
["What are the potential risks in subsurface operations?", None]
|
| 58 |
+
]
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Launch the Gradio interface
|
| 62 |
+
iface.launch()
|
|
|