Spaces:
Sleeping
Sleeping
File size: 2,250 Bytes
6b87b81 a884ec8 f5c9a9d 6b87b81 f5c9a9d 3388e38 f5c9a9d fd671a6 c8d59dd 2313bd3 3388e38 c8d59dd fd671a6 c8d59dd f5c9a9d c8d59dd fd671a6 f5c9a9d c8d59dd f5c9a9d c8d59dd f5c9a9d c8d59dd f5c9a9d c8d59dd fd671a6 c8d59dd f5c9a9d c8d59dd fd671a6 c8d59dd f5c9a9d c8d59dd f5c9a9d c8d59dd f5c9a9d 3388e38 f5c9a9d 3388e38 c8d59dd 4b9952f f5c9a9d 4b9952f c8d59dd 4b9952f 3388e38 c8d59dd 4b9952f 6b87b81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import gradio as gr
import json
import pandas as pd
def analyze_file(message, file):
if not file:
return "Please upload a file."
filepath = file.name
filename = filepath.split("/")[-1]
ext = filename.split(".")[-1].lower()
try:
# Known types
if ext == "jsonl":
with open(filepath, "r", encoding="utf-8") as f:
lines = f.readlines()
data = [json.loads(line) for line in lines]
return f"π JSONL file with *{len(data)}* entries."
elif ext == "json":
with open(filepath, "r", encoding="utf-8") as f:
content = json.load(f)
if isinstance(content, dict):
return f"π JSON with *{len(content.keys())}* top-level keys."
elif isinstance(content, list):
return f"π JSON with *{len(content)}* list items."
else:
return f"π JSON with data type: {type(content)}"
elif ext == "csv":
df = pd.read_csv(filepath)
return f"π CSV with *{df.shape[0]}* rows and *{df.shape[1]}* columns."
elif ext == "py":
with open(filepath, "r", encoding="utf-8") as f:
code = f.read()
return f"π» Python file with *{len(code.splitlines())}* lines of code."
# Unknown types β attempt plain text preview
else:
try:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
preview = content[:1000]
return f"π¦ {ext} file preview:\n\n\n{preview}\n"
except Exception as e:
return f"β Cannot preview this {ext} file. Likely binary. Error: {e}"
except Exception as e:
return f"β Error processing file: {e}"
# Gradio interface
iface = gr.Interface(
fn=analyze_file,
inputs=[
gr.Textbox(label="Message (not used yet)", placeholder="You can ask questions soon"),
gr.File(label="Upload any file")
],
outputs="text",
title="AI Assistant β Universal File Reader",
description="Upload any file (.json, .csv, .py, .txt, .docx, etc). Iβll preview what I can."
)
iface.launch() |