Update app.py
Browse files
app.py
CHANGED
@@ -3,16 +3,16 @@ import fitz # PyMuPDF
|
|
3 |
import requests
|
4 |
from graphviz import Digraph
|
5 |
|
6 |
-
#
|
7 |
def extract_text_from_pdf(pdf_file):
|
8 |
-
pdf_document = fitz.open(
|
9 |
text = ""
|
10 |
for page in pdf_document:
|
11 |
text += page.get_text()
|
12 |
pdf_document.close()
|
13 |
return text
|
14 |
|
15 |
-
#
|
16 |
def search_images(query):
|
17 |
url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
|
18 |
response = requests.get(url)
|
@@ -24,32 +24,23 @@ def search_images(query):
|
|
24 |
images.append(img_url)
|
25 |
return images
|
26 |
|
27 |
-
#
|
28 |
def create_mind_map(text, images):
|
29 |
-
dot = Digraph(
|
30 |
-
dot.node('A', 'Concept Central', shape='
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# Aggiungi un nodo per ogni linea di testo
|
38 |
-
dot.node(f'B{i}', line.strip(), shape='rect', style='filled', fillcolor='lightyellow', fontsize='18')
|
39 |
-
dot.edge('A', f'B{i}', label=f'Subject {i + 1}')
|
40 |
-
|
41 |
-
# Aggiungi le immagini associate
|
42 |
-
if i < len(images):
|
43 |
-
dot.node(f'Image{i}', '', image=images[i], shape='rect', width='2', height='2')
|
44 |
-
dot.edge(f'B{i}', f'Image{i}')
|
45 |
-
|
46 |
return dot
|
47 |
|
48 |
-
#
|
49 |
st.title("Generatore di Mappe Mentali")
|
50 |
st.write("Carica un PDF o inserisci del testo per generare una mappa mentale.")
|
51 |
|
52 |
-
#
|
53 |
input_option = st.radio("Scegli un'opzione:", ('Carica PDF', 'Scrivi Testo'))
|
54 |
if input_option == 'Carica PDF':
|
55 |
uploaded_file = st.file_uploader("Scegli un file PDF...", type="pdf")
|
@@ -59,16 +50,14 @@ if input_option == 'Carica PDF':
|
|
59 |
else:
|
60 |
text = st.text_area("Inserisci il testo:", height=300)
|
61 |
|
62 |
-
#
|
|
|
|
|
|
|
63 |
if st.button("Genera Mappa"):
|
64 |
if text:
|
65 |
-
|
66 |
-
|
67 |
-
images = []
|
68 |
-
for keyword in keywords:
|
69 |
-
images += search_images(keyword) # Aggiungi immagini per ciascuna parola
|
70 |
-
|
71 |
-
mind_map = create_mind_map(text, images)
|
72 |
st.graphviz_chart(mind_map)
|
73 |
else:
|
74 |
st.warning("Per favore, inserisci del testo o carica un PDF.")
|
|
|
3 |
import requests
|
4 |
from graphviz import Digraph
|
5 |
|
6 |
+
# Function to extract text from a PDF
|
7 |
def extract_text_from_pdf(pdf_file):
|
8 |
+
pdf_document = fitz.open(pdf_file)
|
9 |
text = ""
|
10 |
for page in pdf_document:
|
11 |
text += page.get_text()
|
12 |
pdf_document.close()
|
13 |
return text
|
14 |
|
15 |
+
# Function to search images via Wikipedia API
|
16 |
def search_images(query):
|
17 |
url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
|
18 |
response = requests.get(url)
|
|
|
24 |
images.append(img_url)
|
25 |
return images
|
26 |
|
27 |
+
# Function to create a mind map
|
28 |
def create_mind_map(text, images):
|
29 |
+
dot = Digraph()
|
30 |
+
dot.node('A', 'Concept Central', shape='box', style='filled', fillcolor='lightblue')
|
31 |
+
for i, img in enumerate(images):
|
32 |
+
node_label = text[i] if i < len(text) else 'Other'
|
33 |
+
dot.node(f'B{i}', node_label, shape='ellipse', style='filled', fillcolor='lightyellow')
|
34 |
+
dot.edge('A', f'B{i}', label=f'Image {i+1}')
|
35 |
+
dot.node(f'C{i}', img, image=img, shape='rect')
|
36 |
+
dot.edge(f'B{i}', f'C{i}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
return dot
|
38 |
|
39 |
+
# Streamlit app initialization
|
40 |
st.title("Generatore di Mappe Mentali")
|
41 |
st.write("Carica un PDF o inserisci del testo per generare una mappa mentale.")
|
42 |
|
43 |
+
# Input options
|
44 |
input_option = st.radio("Scegli un'opzione:", ('Carica PDF', 'Scrivi Testo'))
|
45 |
if input_option == 'Carica PDF':
|
46 |
uploaded_file = st.file_uploader("Scegli un file PDF...", type="pdf")
|
|
|
50 |
else:
|
51 |
text = st.text_area("Inserisci il testo:", height=300)
|
52 |
|
53 |
+
# Dropdown for map options
|
54 |
+
map_type = st.selectbox("Scegli il tipo di mappa", ["Mappa Concettuale", "Timeline", "Diagramma di Flusso", "Diagramma a Ragnatela"])
|
55 |
+
|
56 |
+
# Button to generate the map
|
57 |
if st.button("Genera Mappa"):
|
58 |
if text:
|
59 |
+
images = search_images(text.split()[:3]) # Use the first three words for the search
|
60 |
+
mind_map = create_mind_map(text.split('\n'), images)
|
|
|
|
|
|
|
|
|
|
|
61 |
st.graphviz_chart(mind_map)
|
62 |
else:
|
63 |
st.warning("Per favore, inserisci del testo o carica un PDF.")
|