mohamedrady commited on
Commit
7769db5
·
verified ·
1 Parent(s): 6969f60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -6,12 +6,12 @@ from transformers import pipeline
6
  import PyPDF2
7
  import gradio as gr
8
 
9
- # Download and initialize required tools
10
  spacy.cli.download("en_core_web_sm")
11
  nlp = spacy.load("en_core_web_sm")
12
  nltk.download('punkt')
13
 
14
- # Check if GPU is available and use it
15
  device = 0 if torch.cuda.is_available() else -1
16
  analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", device=device)
17
 
@@ -47,6 +47,8 @@ def extract_pdf_text(file_path):
47
 
48
  def analyze_and_complete(file_paths):
49
  results = []
 
 
50
  for file_path in file_paths:
51
  if file_path.endswith(".pdf"):
52
  text = extract_pdf_text(file_path)
@@ -54,7 +56,6 @@ def analyze_and_complete(file_paths):
54
  with open(file_path, "r", encoding="utf-8") as file:
55
  text = file.read()
56
 
57
- output_directory = "/Users/Home/Library/Mobile Documents/com~apple~CloudDocs/osa/سيناريوهات/ليالي ألف ليلة"
58
  filename_prefix = os.path.splitext(os.path.basename(file_path))[0]
59
 
60
  spacy_entities = spacy_ner_analysis(text)
@@ -62,14 +63,28 @@ def analyze_and_complete(file_paths):
62
  quotes = nltk_extract_quotes(text)
63
  token_count = count_tokens(text)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  results.append((str(spacy_entities), "\n".join(sentences), "\n".join(quotes), str(token_count)))
 
66
  return results
67
 
68
- # Define the Gradio interface
69
  interface = gr.Interface(
70
  fn=analyze_and_complete,
71
  inputs=gr.File(file_count="multiple", type="filepath"),
72
- outputs=["text", "text", "text", "text"],
73
  title="Movie Script Analyzer and Completer",
74
  description="Upload text, PDF, or DOCX files to analyze and complete the movie script."
75
  )
 
6
  import PyPDF2
7
  import gradio as gr
8
 
9
+ # تحميل وتفعيل الأدوات المطلوبة
10
  spacy.cli.download("en_core_web_sm")
11
  nlp = spacy.load("en_core_web_sm")
12
  nltk.download('punkt')
13
 
14
+ # التحقق من توفر GPU واستخدامه
15
  device = 0 if torch.cuda.is_available() else -1
16
  analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", device=device)
17
 
 
47
 
48
  def analyze_and_complete(file_paths):
49
  results = []
50
+ output_directory = "/Volumes/CLOCKWORK T/clockworkspace/first pro"
51
+
52
  for file_path in file_paths:
53
  if file_path.endswith(".pdf"):
54
  text = extract_pdf_text(file_path)
 
56
  with open(file_path, "r", encoding="utf-8") as file:
57
  text = file.read()
58
 
 
59
  filename_prefix = os.path.splitext(os.path.basename(file_path))[0]
60
 
61
  spacy_entities = spacy_ner_analysis(text)
 
63
  quotes = nltk_extract_quotes(text)
64
  token_count = count_tokens(text)
65
 
66
+ # حفظ النتائج إلى ملفات
67
+ with open(os.path.join(output_directory, f"{filename_prefix}_spacy_entities.txt"), "w", encoding="utf-8") as file:
68
+ file.write(str(spacy_entities))
69
+
70
+ with open(os.path.join(output_directory, f"{filename_prefix}_sentences.txt"), "w", encoding="utf-8") as file:
71
+ file.write("\n".join(sentences))
72
+
73
+ with open(os.path.join(output_directory, f"{filename_prefix}_quotes.txt"), "w", encoding="utf-8") as file:
74
+ file.write("\n".join(quotes))
75
+
76
+ with open(os.path.join(output_directory, f"{filename_prefix}_token_count.txt"), "w", encoding="utf-8") as file:
77
+ file.write(str(token_count))
78
+
79
  results.append((str(spacy_entities), "\n".join(sentences), "\n".join(quotes), str(token_count)))
80
+
81
  return results
82
 
83
+ # تعريف واجهة Gradio
84
  interface = gr.Interface(
85
  fn=analyze_and_complete,
86
  inputs=gr.File(file_count="multiple", type="filepath"),
87
+ outputs=gr.outputs.JSON(),
88
  title="Movie Script Analyzer and Completer",
89
  description="Upload text, PDF, or DOCX files to analyze and complete the movie script."
90
  )