csAhmad commited on
Commit
3bea8a5
·
verified ·
1 Parent(s): dc1d3db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -61
app.py CHANGED
@@ -1,61 +1,55 @@
1
- import gradio as gr
2
- import zipfile
3
- import os
4
- import pandas as pd
5
- from sentence_transformers import SentenceTransformer
6
-
7
- model = SentenceTransformer("csAhmad/zoraiz-model")
8
-
9
- # Create output folder
10
- os.makedirs("temp", exist_ok=True)
11
-
12
- def process_zip(zip_file):
13
- extract_path = "temp/extracted"
14
-
15
- # clean old files
16
- if os.path.exists(extract_path):
17
- for f in os.listdir(extract_path):
18
- os.remove(os.path.join(extract_path, f))
19
- else:
20
- os.makedirs(extract_path)
21
-
22
- # extract zip
23
- with zipfile.ZipFile(zip_file, 'r') as zip_ref:
24
- zip_ref.extractall(extract_path)
25
-
26
- results = []
27
-
28
- # example: read all text files
29
- for file in os.listdir(extract_path):
30
- file_path = os.path.join(extract_path, file)
31
-
32
- if file.endswith(".txt"):
33
- with open(file_path, "r", encoding="utf-8") as f:
34
- text = f.read()
35
-
36
- embedding = model.encode(text)
37
-
38
- results.append({
39
- "file": file,
40
- "embedding_dim": len(embedding),
41
- "first_values": str(embedding[:10])
42
- })
43
-
44
- # create dataframe
45
- df = pd.DataFrame(results)
46
-
47
- output_file = "output.xlsx"
48
- df.to_excel(output_file, index=False)
49
-
50
- return output_file
51
-
52
-
53
- demo = gr.Interface(
54
- fn=process_zip,
55
- inputs=gr.File(file_types=[".zip"]),
56
- outputs=gr.File(label="Download Excel"),
57
- title="ZIP to Excel Processor",
58
- description="Upload ZIP → Extract files → Generate embeddings → Export Excel"
59
- )
60
-
61
- demo.launch(share=True)
 
1
+ import gradio as gr
2
+ import zipfile
3
+ import os
4
+ import pandas as pd
5
+ from sentence_transformers import SentenceTransformer
6
+
7
+ model = SentenceTransformer("csAhmad/zoraiz-model")
8
+
9
+ os.makedirs("temp/extracted", exist_ok=True)
10
+
11
+ def process_zip(zip_file):
12
+ extract_path = "temp/extracted"
13
+ os.makedirs(extract_path, exist_ok=True)
14
+
15
+ zip_path = zip_file.name # always works in Spaces
16
+
17
+ # Extract ZIP
18
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
19
+ zip_ref.extractall(extract_path)
20
+
21
+ results = []
22
+
23
+ # Example: process txt files
24
+ for file in os.listdir(extract_path):
25
+ if file.endswith(".txt"):
26
+ file_path = os.path.join(extract_path, file)
27
+
28
+ with open(file_path, "r", encoding="utf-8") as f:
29
+ text = f.read()
30
+
31
+ emb = model.encode(text)
32
+
33
+ results.append({
34
+ "file": file,
35
+ "embedding_dim": len(emb),
36
+ "first_10_values": str(emb[:10])
37
+ })
38
+
39
+ df = pd.DataFrame(results)
40
+
41
+ output_file = "output.xlsx"
42
+ df.to_excel(output_file, index=False)
43
+
44
+ return output_file
45
+
46
+
47
+ demo = gr.Interface(
48
+ fn=process_zip,
49
+ inputs=gr.File(file_types=[".zip"]),
50
+ outputs=gr.File(label="Download Excel"),
51
+ title="ZIP to Excel Processor",
52
+ description="Upload ZIP → Extract files → Generate embeddings → Export Excel"
53
+ )
54
+
55
+ demo.launch()