rwillats commited on
Commit
5bc0fc9
·
verified ·
1 Parent(s): 4f3bed5

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. test.py +10 -5
test.py CHANGED
@@ -1,10 +1,10 @@
1
  import gradio as gr
2
- import openai
3
  import os
4
  import PyPDF2
5
 
6
- # Read the API key from environment variable (you'll store it on Hugging Face Spaces)
7
- openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
  def read_pdf(file):
10
  if file is None:
@@ -20,28 +20,33 @@ def chat_with_openai(user_input, model_name, uploaded_pdf):
20
  prompt = f"{pdf_text}\n\nUser Query: {user_input}" if pdf_text else user_input
21
 
22
  try:
23
- response = openai.ChatCompletion.create(
24
  model=model_name,
25
  messages=[{"role": "user", "content": prompt}]
26
  )
27
- return response['choices'][0]['message']['content']
28
  except Exception as e:
29
  return f"Error: {str(e)}"
30
 
31
  with gr.Blocks() as app:
32
  gr.Markdown("# 🔥 OpenAI Chat + PDF Analysis Tool")
 
33
  with gr.Row():
34
  model_selector = gr.Dropdown(
35
  choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
36
  label="Select OpenAI Model",
37
  value="gpt-3.5-turbo"
38
  )
 
39
  with gr.Row():
40
  uploaded_pdf = gr.File(label="Upload a PDF (optional)", file_types=[".pdf"])
 
41
  with gr.Row():
42
  user_input = gr.Textbox(label="Your Prompt", placeholder="Ask anything...")
 
43
  with gr.Row():
44
  submit_btn = gr.Button("Submit")
 
45
  with gr.Row():
46
  response_output = gr.Textbox(label="OpenAI Response", lines=10, interactive=True)
47
 
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
  import os
4
  import PyPDF2
5
 
6
+ # Initialize OpenAI client with API key from environment variable
7
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
8
 
9
  def read_pdf(file):
10
  if file is None:
 
20
  prompt = f"{pdf_text}\n\nUser Query: {user_input}" if pdf_text else user_input
21
 
22
  try:
23
+ response = client.chat.completions.create(
24
  model=model_name,
25
  messages=[{"role": "user", "content": prompt}]
26
  )
27
+ return response.choices[0].message.content
28
  except Exception as e:
29
  return f"Error: {str(e)}"
30
 
31
  with gr.Blocks() as app:
32
  gr.Markdown("# 🔥 OpenAI Chat + PDF Analysis Tool")
33
+
34
  with gr.Row():
35
  model_selector = gr.Dropdown(
36
  choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
37
  label="Select OpenAI Model",
38
  value="gpt-3.5-turbo"
39
  )
40
+
41
  with gr.Row():
42
  uploaded_pdf = gr.File(label="Upload a PDF (optional)", file_types=[".pdf"])
43
+
44
  with gr.Row():
45
  user_input = gr.Textbox(label="Your Prompt", placeholder="Ask anything...")
46
+
47
  with gr.Row():
48
  submit_btn = gr.Button("Submit")
49
+
50
  with gr.Row():
51
  response_output = gr.Textbox(label="OpenAI Response", lines=10, interactive=True)
52