Thback commited on
Commit
0595baf
β€’
1 Parent(s): 6e8d2f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -44,14 +44,26 @@ def get_csv_file(csv_docs):
44
  return csv_doc # μΆ”μΆœν•œ ν…μŠ€νŠΈλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
45
 
46
  def get_json_file(json_docs):
 
47
  try:
48
  temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
49
  temp_filepath = os.path.join(temp_dir.name, "temp_file.json") # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
50
  with open(temp_filepath, "wb") as f:
51
  f.write(json_docs.getvalue()) # JSON λ¬Έμ„œμ˜ λ‚΄μš©μ„ μž„μ‹œ νŒŒμΌμ— μ”λ‹ˆλ‹€.
52
- json_loader = JSONLoader(temp_filepath, jq_schema='.messages[].content', text_content=False) # JSONLoaderλ₯Ό μ‚¬μš©ν•΄ JSONλ₯Ό λ‘œλ“œν•©λ‹ˆλ‹€.
53
- json_doc = json_loader.load() # ν…μŠ€νŠΈλ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
54
- return json_doc # μΆ”μΆœν•œ ν…μŠ€νŠΈλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
  st.error(f"Error loading JSON file: {e}")
57
  return []
@@ -59,6 +71,7 @@ def get_json_file(json_docs):
59
 
60
 
61
 
 
62
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
63
  def get_text_chunks(documents):
64
  text_splitter = RecursiveCharacterTextSplitter(
 
44
  return csv_doc # μΆ”μΆœν•œ ν…μŠ€νŠΈλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
45
 
46
  def get_json_file(json_docs):
47
+ def get_json_file(json_docs):
48
  try:
49
  temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
50
  temp_filepath = os.path.join(temp_dir.name, "temp_file.json") # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
51
  with open(temp_filepath, "wb") as f:
52
  f.write(json_docs.getvalue()) # JSON λ¬Έμ„œμ˜ λ‚΄μš©μ„ μž„μ‹œ νŒŒμΌμ— μ”λ‹ˆλ‹€.
53
+
54
+ # Load JSON file
55
+ json_loader = JSONLoader(temp_filepath, jq_schema='.messages[].content', text_content=False)
56
+ json_doc = json_loader.load()
57
+
58
+ # Remove empty chunks
59
+ non_empty_chunks = [chunk for chunk in json_doc if len(chunk) > 0]
60
+
61
+ # Check if any non-empty chunks are present
62
+ if not non_empty_chunks:
63
+ st.error("No non-empty chunks found in the JSON file.")
64
+ return []
65
+
66
+ return non_empty_chunks
67
  except Exception as e:
68
  st.error(f"Error loading JSON file: {e}")
69
  return []
 
71
 
72
 
73
 
74
+
75
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
76
  def get_text_chunks(documents):
77
  text_splitter = RecursiveCharacterTextSplitter(