Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,92 @@
|
|
1 |
import os
|
2 |
-
import logging
|
3 |
-
import csv
|
4 |
-
import shutil
|
5 |
import nltk
|
6 |
-
import
|
|
|
7 |
from tqdm import tqdm
|
8 |
import gradio as gr
|
9 |
-
from datasets import Dataset
|
10 |
from transformers import pipeline
|
11 |
-
from huggingface_hub import HfApi
|
12 |
-
|
13 |
-
#
|
14 |
-
logging.basicConfig(
|
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 |
-
def process_words(model_name, limit=None):
|
45 |
-
logging.info("Initializing Hugging Face text2text-generation pipeline...")
|
46 |
-
generator = pipeline("text2text-generation", model=model_name, device=-1)
|
47 |
-
words_list = get_all_words()
|
48 |
-
if limit:
|
49 |
-
words_list = words_list[:limit]
|
50 |
-
data = []
|
51 |
-
for word in tqdm(words_list, desc="Processing words"):
|
52 |
-
tokens = nltk.word_tokenize(word)
|
53 |
-
meaning = generate_meaning(word, generator)
|
54 |
-
data.append({
|
55 |
-
"tokenizer": tokens,
|
56 |
-
"words": word,
|
57 |
-
"meaning": meaning
|
58 |
-
})
|
59 |
-
logging.info("Finished processing words.")
|
60 |
-
return data
|
61 |
|
62 |
def save_to_csv(data, filename="output.csv"):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
data
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
def about_tab_content():
|
99 |
-
about_text = (
|
100 |
-
"## DeepFocus-X3 Dataset Generator\n\n"
|
101 |
-
"This tool downloads all available words from the NLTK corpus, "
|
102 |
-
"generates concise meanings using a Hugging Face text-to-text generation model, "
|
103 |
-
"and converts the data into a CSV file. Finally, it pushes the CSV to the "
|
104 |
-
"[katsukiai/DeepFocus-X3](https://huggingface.co/datasets/katsukiai/DeepFocus-X3) repository."
|
105 |
-
)
|
106 |
-
return about_text
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
114 |
)
|
115 |
-
|
116 |
-
|
117 |
-
# ---------------------- Gradio App ----------------------
|
118 |
-
with gr.Blocks() as demo:
|
119 |
-
gr.Markdown("## DeepFocus-X3 Dataset Generator")
|
120 |
-
|
121 |
-
with gr.Tabs():
|
122 |
-
# About Tab
|
123 |
-
with gr.Tab("About"):
|
124 |
-
gr.Markdown(about_tab_content())
|
125 |
-
|
126 |
-
# Generate All Tab
|
127 |
-
with gr.Tab("Generate all"):
|
128 |
-
model_name_input = gr.Textbox(value="google/flan-t5-xl", label="Hugging Face Model Name for Means")
|
129 |
-
word_limit_input = gr.Textbox(value="50", label="Word Limit (Leave empty for all)")
|
130 |
-
generate_button = gr.Button("Generate and Push Dataset")
|
131 |
-
generate_output = gr.Textbox(label="Output")
|
132 |
-
generate_button.click(run_generate, inputs=[model_name_input, word_limit_input], outputs=generate_output)
|
133 |
-
|
134 |
-
# Settings Tab
|
135 |
-
with gr.Tab("Settings"):
|
136 |
-
gr.Markdown(settings_tab_content())
|
137 |
|
138 |
-
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import nltk
|
3 |
+
import csv
|
4 |
+
import logging
|
5 |
from tqdm import tqdm
|
6 |
import gradio as gr
|
|
|
7 |
from transformers import pipeline
|
8 |
+
from huggingface_hub import HfApi, upload_file
|
9 |
+
|
10 |
+
# Setup Logging
|
11 |
+
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
+
|
13 |
+
# Download NLTK Data
|
14 |
+
nltk.download('punkt')
|
15 |
+
|
16 |
+
# Constants
|
17 |
+
HF_REPO = "katsukiai/DeepFocus-X3"
|
18 |
+
TOKENIZER = 'bert-base-uncased'
|
19 |
+
MODELS = ["bert-base-uncased", "gpt2", "roberta-base", "distilbert-base-uncased", "albert-base-v2"] # Add more models as needed
|
20 |
+
|
21 |
+
# Initialize Models
|
22 |
+
models = {model: pipeline('feature-extraction', model=model) for model in MODELS}
|
23 |
+
|
24 |
+
# Functions
|
25 |
+
def process_text(text):
|
26 |
+
tokens = nltk.word_tokenize(text)
|
27 |
+
words = list(set(tokens))
|
28 |
+
means = {}
|
29 |
+
for word in tqdm(words, desc="Processing Words"):
|
30 |
+
word_means = {}
|
31 |
+
for model_name, model in models.items():
|
32 |
+
try:
|
33 |
+
output = model(word)
|
34 |
+
word_means[model_name] = output[0].mean().item()
|
35 |
+
except Exception as e:
|
36 |
+
logging.error(f"Error processing word {word} with model {model_name}: {e}")
|
37 |
+
word_means[model_name] = None
|
38 |
+
means[word] = word_means
|
39 |
+
return {"tokenizer": tokens, "words": words, "meaning": means}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
def save_to_csv(data, filename="output.csv"):
|
42 |
+
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
|
43 |
+
writer = csv.DictWriter(csvfile, fieldnames=["word", "tokenizer", "meanings"])
|
44 |
+
writer.writeheader()
|
45 |
+
for word in data['words']:
|
46 |
+
writer.writerow({
|
47 |
+
"word": word,
|
48 |
+
"tokenizer": data['tokenizer'],
|
49 |
+
"meanings": str(data['meaning'][word])
|
50 |
+
})
|
51 |
+
|
52 |
+
def train_dataset():
|
53 |
+
text = "Your long text goes here..."
|
54 |
+
data = process_text(text)
|
55 |
+
save_to_csv(data)
|
56 |
+
logging.info("Dataset processed and saved to CSV.")
|
57 |
+
|
58 |
+
def generate_report():
|
59 |
+
with open('app.log', 'r') as log_file:
|
60 |
+
log_content = log_file.read()
|
61 |
+
return log_content
|
62 |
+
|
63 |
+
# Gradio Interface
|
64 |
+
def generate_all(text):
|
65 |
+
data = process_text(text)
|
66 |
+
save_to_csv(data)
|
67 |
+
return f"Processed data saved to output.csv"
|
68 |
+
|
69 |
+
iface = gr.Interface(
|
70 |
+
fn=[generate_all, generate_report],
|
71 |
+
inputs="text",
|
72 |
+
outputs=["text", "text"],
|
73 |
+
title="DeepFocus-X3",
|
74 |
+
tab_titles=["Generate All", "Logs"],
|
75 |
+
description="Generate processed data and view logs."
|
76 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
# Run and Push to HuggingFace
|
79 |
+
def run_and_push():
|
80 |
+
train_dataset()
|
81 |
+
api = HfApi()
|
82 |
+
api.create_repo(repo_id=HF_REPO, private=False, exist_ok=True)
|
83 |
+
upload_file(
|
84 |
+
path_or_fileobj="output.csv",
|
85 |
+
path_in_repo="output.csv",
|
86 |
+
repo_id=HF_REPO
|
87 |
)
|
88 |
+
logging.info("Dataset pushed to HuggingFace.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
if __name__ == "__main__":
|
91 |
+
iface.launch()
|
92 |
+
run_and_push()
|