Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 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"π
|
| 18 |
|
| 19 |
-
elif
|
| 20 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 21 |
content = json.load(f)
|
| 22 |
if isinstance(content, dict):
|
| 23 |
-
return f"π JSON
|
| 24 |
elif isinstance(content, list):
|
| 25 |
-
return f"π JSON
|
| 26 |
else:
|
| 27 |
-
return f"π JSON
|
| 28 |
|
| 29 |
-
elif
|
| 30 |
df = pd.read_csv(filepath)
|
| 31 |
-
return f"π CSV
|
| 32 |
|
| 33 |
-
elif
|
| 34 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 35 |
-
|
| 36 |
-
return f"π» Python file with *{len(
|
| 37 |
|
|
|
|
| 38 |
else:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="
|
| 48 |
-
gr.File(label="Upload
|
| 49 |
],
|
| 50 |
outputs="text",
|
| 51 |
-
title="AI Assistant β
|
| 52 |
-
description="Upload
|
| 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()
|