dindizz commited on
Commit
663e0a3
·
verified ·
1 Parent(s): 418e7e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -59
app.py CHANGED
@@ -1,63 +1,62 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
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
- if __name__ == "__main__":
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()