han-byeol commited on
Commit
9731345
ยท
1 Parent(s): 5d55a9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -52,27 +52,25 @@ def get_csv_file(csv_docs):
52
 
53
 
54
  # json ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
55
-
56
- def extract_text_from_json_file(file_path):
57
- try:
58
- with open(file_path, 'r') as file:
59
- data = json.load(file)
60
-
61
- # 'users' ํ‚ค์˜ ๊ฐ’์„ ์ถ”์ถœ
62
- users_data = data.get('users', [])
63
-
64
- # 'users' ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์—์„œ 'firstName' ํ‚ค์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ
65
- first_names = [user.get('firstName', '') for user in users_data]
66
-
67
- # ์ถ”์ถœํ•œ ๊ฐ’ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
68
- text = ', '.join(first_names)
69
- return text
70
-
71
- except FileNotFoundError:
72
- return f"File not found: {file_path}"
73
- except json.JSONDecodeError as e:
74
- return f"Invalid JSON format: {e}"
75
-
76
 
77
 
78
 
 
52
 
53
 
54
  # json ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
55
+ class JSONLoader:
56
+ def __init__(self, file_path, jq_schema):
57
+ self.file_path = file_path
58
+ self.jq_schema = jq_schema
59
+
60
+ def load(self):
61
+ with open(self.file_path, 'r') as json_file:
62
+ data = json.load(json_file)
63
+ users_text = [user.get('text', '') for user in data.get('users', [])]
64
+ return users_text
65
+
66
+ def get_json_file(json_docs):
67
+ temp_dir = tempfile.TemporaryDirectory()
68
+ temp_filepath = os.path.join(temp_dir.name, json_docs.name)
69
+ with open(temp_filepath, "wb") as f:
70
+ f.write(json_docs.getvalue())
71
+ json_loader = JSONLoader(temp_filepath, jq_schema={})
72
+ users_text = json_loader.load()
73
+ return users_text
 
 
74
 
75
 
76