rwillats commited on
Commit
267c6ea
·
verified ·
1 Parent(s): 5bc0fc9

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. test.py +5 -4
test.py CHANGED
@@ -3,8 +3,8 @@ 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:
@@ -16,12 +16,13 @@ def read_pdf(file):
16
  return text
17
 
18
  def chat_with_openai(user_input, model_name, uploaded_pdf):
 
19
  pdf_text = read_pdf(uploaded_pdf) if uploaded_pdf else ""
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
@@ -52,7 +53,7 @@ with gr.Blocks() as app:
52
 
53
  submit_btn.click(
54
  fn=chat_with_openai,
55
- inputs=[user_input, model_selector, uploaded_pdf],
56
  outputs=response_output
57
  )
58
 
 
3
  import os
4
  import PyPDF2
5
 
6
+ # Initialize OpenAI client ONCE
7
+ client = OpenAI(api_key=os.getenv("OPENAI_KEY")) # Note: Corrected to OPENAI_KEY here
8
 
9
  def read_pdf(file):
10
  if file is None:
 
16
  return text
17
 
18
  def chat_with_openai(user_input, model_name, uploaded_pdf):
19
+ # model_name comes correctly from dropdown selection
20
  pdf_text = read_pdf(uploaded_pdf) if uploaded_pdf else ""
21
  prompt = f"{pdf_text}\n\nUser Query: {user_input}" if pdf_text else user_input
22
 
23
  try:
24
  response = client.chat.completions.create(
25
+ model=model_name, # 🔥 Use selected model here
26
  messages=[{"role": "user", "content": prompt}]
27
  )
28
  return response.choices[0].message.content
 
53
 
54
  submit_btn.click(
55
  fn=chat_with_openai,
56
+ inputs=[user_input, model_selector, uploaded_pdf], # Make sure model_selector is passed here
57
  outputs=response_output
58
  )
59