Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,198 +1,24 @@
|
|
1 |
-
import os
|
2 |
-
import re
|
3 |
-
from camel_tools.tokenizers.word import simple_word_tokenize
|
4 |
-
from camel_tools.ner import NERecognizer
|
5 |
-
import nltk
|
6 |
-
import torch
|
7 |
-
from collections import Counter
|
8 |
-
from transformers import pipeline, AutoModel, AutoTokenizer
|
9 |
-
import PyPDF2
|
10 |
import gradio as gr
|
11 |
-
import
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
arabic_gpt2_model = AutoModel.from_pretrained("aubmindlab/aragpt2-base")
|
31 |
-
|
32 |
-
arabic_electra_tokenizer = AutoTokenizer.from_pretrained("aubmindlab/araelectra-base-discriminator")
|
33 |
-
arabic_electra_model = AutoModel.from_pretrained("aubmindlab/araelectra-base-discriminator")
|
34 |
-
|
35 |
-
arabert_tokenizer = AutoTokenizer.from_pretrained("aubmindlab/bert-base-arabertv02")
|
36 |
-
arabert_model = AutoModel.from_pretrained("aubmindlab/bert-base-arabertv02")
|
37 |
-
|
38 |
-
# دالة لتحليل النص باستخدام camel_tools
|
39 |
-
def camel_ner_analysis(text):
|
40 |
-
ner = NERecognizer.pretrained()
|
41 |
-
tokens = simple_word_tokenize(text)
|
42 |
-
entities = ner.predict(tokens)
|
43 |
-
entity_dict = {"PERSON": [], "LOC": [], "ORG": [], "DATE": []}
|
44 |
-
for token, tag in zip(tokens, entities):
|
45 |
-
if tag in entity_dict:
|
46 |
-
entity_dict[tag].append((token, tag))
|
47 |
-
return entity_dict
|
48 |
-
|
49 |
-
# دالة لتحليل المشاعر
|
50 |
-
def analyze_sentiments(text):
|
51 |
-
sentiments = analyzer(text)
|
52 |
-
return sentiments
|
53 |
-
|
54 |
-
# دالة لتجزئة النص إلى جمل
|
55 |
-
def nltk_extract_sentences(text):
|
56 |
-
sentences = nltk.tokenize.sent_tokenize(text, language='arabic')
|
57 |
-
return sentences
|
58 |
-
|
59 |
-
# دالة لاستخراج الاقتباسات من النص
|
60 |
-
def nltk_extract_quotes(text):
|
61 |
-
quotes = []
|
62 |
-
sentences = nltk.tokenize.sent_tokenize(text, language='arabic')
|
63 |
-
for sentence in sentences:
|
64 |
-
if '"' in sentence or '«' in sentence or '»' in sentence:
|
65 |
-
quotes.append(sentence)
|
66 |
-
return quotes
|
67 |
-
|
68 |
-
# دالة لعد الرموز في النص
|
69 |
-
def count_tokens(text):
|
70 |
-
tokens = simple_word_tokenize(text)
|
71 |
-
return len(tokens)
|
72 |
-
|
73 |
-
# دالة لاستخراج النص من ملفات PDF
|
74 |
-
def extract_pdf_text(file_path):
|
75 |
-
with open(file_path, "rb") as pdf_file:
|
76 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
77 |
-
text = ""
|
78 |
-
for page_num in range(len(pdf_reader.pages)):
|
79 |
-
page = pdf_reader.pages[page_num]
|
80 |
-
text += page.extract_text()
|
81 |
-
return text
|
82 |
-
|
83 |
-
# دالة لاستخراج المشاهد من النص
|
84 |
-
def extract_scenes(text):
|
85 |
-
scenes = re.split(r'داخلي|خارجي', text)
|
86 |
-
scenes = [scene.strip() for scene in scenes if scene.strip()]
|
87 |
-
return scenes
|
88 |
-
|
89 |
-
# دالة لاستخراج تفاصيل المشهد (المكان والوقت)
|
90 |
-
def extract_scene_details(scene):
|
91 |
-
details = {}
|
92 |
-
location_match = re.search(r'(داخلي|خارجي)', scene)
|
93 |
-
time_match = re.search(r'(ليلاً|نهاراً|شروق|غروب)', scene)
|
94 |
-
|
95 |
-
if location_match:
|
96 |
-
details['location'] = location_match.group()
|
97 |
-
if time_match:
|
98 |
-
details['time'] = time_match.group()
|
99 |
-
|
100 |
-
return details
|
101 |
-
|
102 |
-
# دالة لاستخراج أعمار الشخصيات
|
103 |
-
def extract_ages(text):
|
104 |
-
ages = re.findall(r'\b(\d{1,2})\s*(?:عام|سنة|سنوات)\s*(?:من العمر|عمره|عمرها)', text)
|
105 |
-
return ages
|
106 |
-
|
107 |
-
# دالة لاستخراج وصف الشخصيات
|
108 |
-
def extract_character_descriptions(text):
|
109 |
-
descriptions = re.findall(r'شخصية\s*(.*?)\s*:\s*وصف\s*(.*?)\s*(?:\.|،)', text, re.DOTALL)
|
110 |
-
return descriptions
|
111 |
-
|
112 |
-
# دالة لاستخراج تكرار الشخصيات
|
113 |
-
def extract_character_frequency(entities):
|
114 |
-
persons = [ent[0] for ent in entities['PERSON']]
|
115 |
-
frequency = Counter(persons)
|
116 |
-
return frequency
|
117 |
-
|
118 |
-
# دالة لاستخراج الحوارات وتحديد المتحدثين
|
119 |
-
def extract_dialogues(text):
|
120 |
-
dialogues = re.findall(r'(.*?)(?:\s*:\s*)(.*?)(?=\n|$)', text, re.DOTALL)
|
121 |
-
return dialogues
|
122 |
-
|
123 |
-
# دالة لتحليل النصوص واستخراج المعلومات وحفظ النتائج
|
124 |
-
def analyze_and_complete(file_paths):
|
125 |
-
results = []
|
126 |
-
output_directory = os.getenv("SPACE_DIR", "/app/output")
|
127 |
-
|
128 |
-
for file_path in file_paths:
|
129 |
-
if file_path.endswith(".pdf"):
|
130 |
-
text = extract_pdf_text(file_path)
|
131 |
-
else:
|
132 |
-
with open(file_path, "r", encoding="utf-8") as file:
|
133 |
-
text = file.read()
|
134 |
-
|
135 |
-
filename_prefix = os.path.splitext(os.path.basename(file_path))[0]
|
136 |
-
|
137 |
-
camel_entities = camel_ner_analysis(text)
|
138 |
-
sentiments = analyze_sentiments(text)
|
139 |
-
sentences = nltk_extract_sentences(text)
|
140 |
-
quotes = nltk_extract_quotes(text)
|
141 |
-
token_count = count_tokens(text)
|
142 |
-
scenes = extract_scenes(text)
|
143 |
-
ages = extract_ages(text)
|
144 |
-
character_descriptions = extract_character_descriptions(text)
|
145 |
-
character_frequency = extract_character_frequency(camel_entities)
|
146 |
-
dialogues = extract_dialogues(text)
|
147 |
-
|
148 |
-
scene_details = [extract_scene_details(scene) for scene in scenes]
|
149 |
-
|
150 |
-
# حفظ النتائج إلى ملفات
|
151 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_entities.txt"), "w", encoding="utf-8") as file:
|
152 |
-
file.write(str(camel_entities))
|
153 |
-
|
154 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_sentiments.txt"), "w", encoding="utf-8") as file:
|
155 |
-
file.write(str(sentiments))
|
156 |
-
|
157 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_sentences.txt"), "w", encoding="utf-8") as file:
|
158 |
-
file.write("\n".join(sentences))
|
159 |
-
|
160 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_quotes.txt"), "w", encoding="utf-8") as file:
|
161 |
-
file.write("\n".join(quotes))
|
162 |
-
|
163 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_token_count.txt"), "w", encoding="utf-8") as file:
|
164 |
-
file.write(str(token_count))
|
165 |
-
|
166 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_scenes.txt"), "w", encoding="utf-8") as file:
|
167 |
-
file.write("\n".join(scenes))
|
168 |
-
|
169 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_scene_details.txt"), "w", encoding="utf-8") as file:
|
170 |
-
file.write(str(scene_details))
|
171 |
-
|
172 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_ages.txt"), "w", encoding="utf-8") as file:
|
173 |
-
file.write(str(ages))
|
174 |
-
|
175 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_character_descriptions.txt"), "w", encoding="utf-8") as file:
|
176 |
-
file.write(str(character_descriptions))
|
177 |
-
|
178 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_character_frequency.txt"), "w", encoding="utf-8") as file:
|
179 |
-
file.write(str(character_frequency))
|
180 |
-
|
181 |
-
with open(os.path.join(output_directory, f"{filename_prefix}_dialogues.txt"), "w", encoding="utf-8") as file:
|
182 |
-
file.write(str(dialogues))
|
183 |
-
|
184 |
-
results.append((str(camel_entities), str(sentiments), "\n".join(sentences), "\n".join(quotes), str(token_count), "\n".join(scenes), str(scene_details), str(ages), str(character_descriptions), str(character_frequency), str(dialogues)))
|
185 |
-
|
186 |
-
return results
|
187 |
-
|
188 |
-
## تعريف واجهة Gradio
|
189 |
-
interface = gr.Interface(
|
190 |
-
fn=analyze_and_complete,
|
191 |
-
inputs=gr.File(file_count="multiple", type="filepath"),
|
192 |
-
outputs=gr.JSON(),
|
193 |
-
title="Movie Script Analyzer and Completer",
|
194 |
-
description="Upload text, PDF, or DOCX files to analyze and complete the movie script."
|
195 |
)
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
def run_scripts():
|
5 |
+
outputs = []
|
6 |
+
scripts = ["firstkha.py", "alf.py"]
|
7 |
+
for script in scripts:
|
8 |
+
try:
|
9 |
+
result = subprocess.run(["python", script], check=True, text=True, capture_output=True)
|
10 |
+
outputs.append(f"Output of {script}:\n{result.stdout}")
|
11 |
+
except subprocess.CalledProcessError as e:
|
12 |
+
outputs.append(f"Error running {script}:\n{e.stderr}")
|
13 |
+
return "\n\n".join(outputs)
|
14 |
+
|
15 |
+
iface = gr.Interface(
|
16 |
+
fn=run_scripts,
|
17 |
+
inputs=[],
|
18 |
+
outputs="text",
|
19 |
+
live=True,
|
20 |
+
description="Run the scripts firstkha.py and alf.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
|
23 |
if __name__ == "__main__":
|
24 |
+
iface.launch()
|