cicboy commited on
Commit
918044e
·
1 Parent(s): 7487cde

Fix OpenAI client initialization and PDF handling

Browse files
Files changed (1) hide show
  1. app.py +9 -1
app.py CHANGED
@@ -16,6 +16,8 @@ WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY")
16
  print("Testing Weaviate connection...")
17
  print("URL:", WEAVIATE_URL)
18
  print("API KEY:", "SET" if WEAVIATE_API_KEY else "MISSING")
 
 
19
 
20
  # Connect to Weaviate Cloud
21
  client = weaviate.connect_to_weaviate_cloud(
@@ -24,8 +26,12 @@ client = weaviate.connect_to_weaviate_cloud(
24
  skip_init_checks=True
25
  )
26
 
 
 
27
  # Load and process PDF
28
  def extract_text_from_pdf(pdf_path):
 
 
29
  reader = PdfReader(pdf_path)
30
  text = ""
31
  for page in reader.pages:
@@ -104,6 +110,8 @@ def ask_question(query):
104
  #Gradio App
105
  def process_pdf(pdf_file):
106
  try:
 
 
107
  setup_schema()
108
  # pdf_file is already a string path because of type="filepath"
109
  text = extract_text_from_pdf(pdf_file)
@@ -133,5 +141,5 @@ with gr.Blocks() as demo:
133
  ask_btn = gr.Button("Ask")
134
  ask_btn.click(qa_pipeline, inputs=question, outputs=answer)
135
 
136
- demo.launch()
137
  client.close()
 
16
  print("Testing Weaviate connection...")
17
  print("URL:", WEAVIATE_URL)
18
  print("API KEY:", "SET" if WEAVIATE_API_KEY else "MISSING")
19
+ print("OPENAI_API_KEY:", "SET" if OPENAI_API_KEY else "MISSING")
20
+
21
 
22
  # Connect to Weaviate Cloud
23
  client = weaviate.connect_to_weaviate_cloud(
 
26
  skip_init_checks=True
27
  )
28
 
29
+ openai_client = OpenAI(api_key=OPENAI_API_KEY)
30
+
31
  # Load and process PDF
32
  def extract_text_from_pdf(pdf_path):
33
+ if not pdf_path or not os.path.exists(pdf_path):
34
+ raise ValueError(f"No PDF file provided")
35
  reader = PdfReader(pdf_path)
36
  text = ""
37
  for page in reader.pages:
 
110
  #Gradio App
111
  def process_pdf(pdf_file):
112
  try:
113
+ if not pdf_file:
114
+ return "❌ No file uploaded"
115
  setup_schema()
116
  # pdf_file is already a string path because of type="filepath"
117
  text = extract_text_from_pdf(pdf_file)
 
141
  ask_btn = gr.Button("Ask")
142
  ask_btn.click(qa_pipeline, inputs=question, outputs=answer)
143
 
144
+ demo.launch(share=True)
145
  client.close()