Percy3822 commited on
Commit
c8d59dd
Β·
verified Β·
1 Parent(s): fd671a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -6,50 +6,60 @@ def analyze_file(message, file):
6
  if not file:
7
  return "Please upload a file."
8
 
9
- # Get file path string instead of using file.read()
10
  filepath = file.name
 
 
11
 
12
  try:
13
- if filepath.endswith(".jsonl"):
 
14
  with open(filepath, "r", encoding="utf-8") as f:
15
  lines = f.readlines()
16
  data = [json.loads(line) for line in lines]
17
- return f"πŸ“„ This is a JSONL file with *{len(data)}* entries."
18
 
19
- elif filepath.endswith(".json"):
20
  with open(filepath, "r", encoding="utf-8") as f:
21
  content = json.load(f)
22
  if isinstance(content, dict):
23
- return f"πŸ“ JSON file with *{len(content.keys())}* top-level keys."
24
  elif isinstance(content, list):
25
- return f"πŸ“ JSON file with *{len(content)}* items in a list."
26
  else:
27
- return f"πŸŒ€ JSON file with data type: {type(content)}"
28
 
29
- elif filepath.endswith(".csv"):
30
  df = pd.read_csv(filepath)
31
- return f"πŸ“Š CSV file with *{df.shape[0]}* rows and *{df.shape[1]}* columns."
32
 
33
- elif filepath.endswith(".py"):
34
  with open(filepath, "r", encoding="utf-8") as f:
35
- lines = f.readlines()
36
- return f"πŸ’» Python file with *{len(lines)}* lines of code."
37
 
 
38
  else:
39
- return f"⚠ Unsupported file type: {filepath}. Try .jsonl, .json, .csv, or .py."
 
 
 
 
 
 
40
 
41
  except Exception as e:
42
  return f"❌ Error processing file: {e}"
43
 
 
44
  iface = gr.Interface(
45
  fn=analyze_file,
46
  inputs=[
47
- gr.Textbox(label="Message (not used yet)", placeholder="e.g. How many questions?"),
48
- gr.File(label="Upload a file", file_types=[".jsonl", ".json", ".csv", ".py"])
49
  ],
50
  outputs="text",
51
- title="AI Assistant – Multi-File Handler",
52
- description="Upload a .jsonl, .json, .csv, or .py file and I’ll tell you what’s inside."
53
  )
54
 
55
  iface.launch()
 
6
  if not file:
7
  return "Please upload a file."
8
 
 
9
  filepath = file.name
10
+ filename = filepath.split("/")[-1]
11
+ ext = filename.split(".")[-1].lower()
12
 
13
  try:
14
+ # Known types
15
+ if ext == "jsonl":
16
  with open(filepath, "r", encoding="utf-8") as f:
17
  lines = f.readlines()
18
  data = [json.loads(line) for line in lines]
19
+ return f"πŸ“„ JSONL file with *{len(data)}* entries."
20
 
21
+ elif ext == "json":
22
  with open(filepath, "r", encoding="utf-8") as f:
23
  content = json.load(f)
24
  if isinstance(content, dict):
25
+ return f"πŸ“ JSON with *{len(content.keys())}* top-level keys."
26
  elif isinstance(content, list):
27
+ return f"πŸ“ JSON with *{len(content)}* list items."
28
  else:
29
+ return f"πŸŒ€ JSON with data type: {type(content)}"
30
 
31
+ elif ext == "csv":
32
  df = pd.read_csv(filepath)
33
+ return f"πŸ“Š CSV with *{df.shape[0]}* rows and *{df.shape[1]}* columns."
34
 
35
+ elif ext == "py":
36
  with open(filepath, "r", encoding="utf-8") as f:
37
+ code = f.read()
38
+ return f"πŸ’» Python file with *{len(code.splitlines())}* lines of code."
39
 
40
+ # Unknown types – attempt plain text preview
41
  else:
42
+ try:
43
+ with open(filepath, "r", encoding="utf-8") as f:
44
+ content = f.read()
45
+ preview = content[:1000]
46
+ return f"πŸ“¦ {ext} file preview:\n\n\n{preview}\n"
47
+ except Exception as e:
48
+ return f"❌ Cannot preview this {ext} file. Likely binary. Error: {e}"
49
 
50
  except Exception as e:
51
  return f"❌ Error processing file: {e}"
52
 
53
+ # Gradio interface
54
  iface = gr.Interface(
55
  fn=analyze_file,
56
  inputs=[
57
+ gr.Textbox(label="Message (not used yet)", placeholder="You can ask questions soon"),
58
+ gr.File(label="Upload any file")
59
  ],
60
  outputs="text",
61
+ title="AI Assistant – Universal File Reader",
62
+ description="Upload any file (.json, .csv, .py, .txt, .docx, etc). I’ll preview what I can."
63
  )
64
 
65
  iface.launch()