Till Fischer commited on
Commit
2f86969
·
1 Parent(s): b3af7d8

Add Gradio app

Browse files
Files changed (2) hide show
  1. app.py +35 -42
  2. aspect-sentiment-analyzer +1 -0
app.py CHANGED
@@ -1,56 +1,49 @@
 
 
1
  import gradio as gr
 
2
  from pathlib import Path
 
3
  import shutil
4
- import uuid
5
- import subprocess
6
-
7
- def analyze_sqlite(sqlite_file, isbn, languages):
8
- # Arbeitsverzeichnis vorbereiten
9
- run_id = str(uuid.uuid4())
10
- workdir = Path(f"/tmp/{run_id}")
11
- output_dir = Path("output")
12
- workdir.mkdir(parents=True, exist_ok=True)
13
- output_dir.mkdir(parents=True, exist_ok=True)
14
-
15
- # DB-Datei speichern
16
- db_path = workdir / "buch_datenbank.sqlite"
17
- shutil.copy(sqlite_file.name, db_path)
18
-
19
- # Sprachliste korrekt übergeben
20
- lang_args = []
21
- for lang in languages:
22
- lang_args.append(lang)
23
-
24
- try:
25
- subprocess.run(
26
- [
27
- "python3", "analyze_aspects.py",
28
- "--db-path", str(db_path),
29
- "--isbn", isbn,
30
- "--languages", *lang_args
31
- ],
32
- check=True
33
  )
34
- except subprocess.CalledProcessError as e:
35
- return f"Analyse fehlgeschlagen: {e}", None
36
 
37
- chart_path = output_dir / "sentiment_aspekte.png"
38
- return "Analyse abgeschlossen ✅", str(chart_path) if chart_path.exists() else None
 
 
 
 
 
39
 
 
40
  iface = gr.Interface(
41
- fn=analyze_sqlite,
42
  inputs=[
43
- gr.File(label="SQLite-Datenbank (.sqlite)"),
44
- gr.Textbox(label="ISBN", placeholder="9783446264199"),
45
- gr.CheckboxGroup(["de", "en"], label="Sprachen", value=["de"])
46
  ],
47
  outputs=[
48
  gr.Text(label="Status"),
49
- gr.Image(type="filepath", label="Sentiment-Diagramm")
50
  ],
51
- title="Aspekt-Sentimentanalyse",
52
- description="Lade eine bereinigte SQLite-Datenbank hoch und erhalte eine visuelle Analyse der Sentiment-Scores nach Aspekten."
53
  )
54
 
55
- if __name__ == "__main__":
56
- iface.launch()
 
1
+ # app.py
2
+
3
  import gradio as gr
4
+ from analyze_aspects import analyze_quickwin, visualize_aspects
5
  from pathlib import Path
6
+ import tempfile
7
  import shutil
8
+
9
+ def run_analysis(db_file, isbn, languages):
10
+ if not isbn.strip():
11
+ return "❗ Bitte ISBN angeben.", None
12
+
13
+ with tempfile.TemporaryDirectory() as tmpdir:
14
+ tmp_path = Path(tmpdir) / "db.sqlite"
15
+ shutil.copy(db_file.name, tmp_path)
16
+
17
+ # Analyse
18
+ results = analyze_quickwin(
19
+ db_path=tmp_path,
20
+ isbn=isbn,
21
+ device=-1,
22
+ languages=languages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  )
 
 
24
 
25
+ if not results:
26
+ return "⚠️ Keine relevanten Aspekte gefunden oder Fehler in der Analyse.", None
27
+
28
+ # Diagramm erzeugen
29
+ visualize_aspects(results, Path(tmpdir))
30
+ chart_path = Path(tmpdir) / "sentiment_aspekte.png"
31
+ return "✅ Analyse abgeschlossen!", chart_path
32
 
33
+ # Gradio Interface
34
  iface = gr.Interface(
35
+ fn=run_analysis,
36
  inputs=[
37
+ gr.File(label="SQLite-Datenbank (.sqlite)", file_types=[".sqlite"]),
38
+ gr.Text(label="ISBN", placeholder="z. B. 9783446264199"),
39
+ gr.CheckboxGroup(choices=["de", "en"], label="Sprachen", value=["de"])
40
  ],
41
  outputs=[
42
  gr.Text(label="Status"),
43
+ gr.Image(label="Sentiment-Diagramm", type="filepath")
44
  ],
45
+ title="📖 Aspekt-Sentiment-Analyse",
46
+ description="Lade eine SQLite-Datenbank hoch, gib eine ISBN an und analysiere die wichtigsten inhaltlichen Aspekte und deren Sentiment."
47
  )
48
 
49
+ iface.launch()
 
aspect-sentiment-analyzer ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit de51e3830863b035c34001bde80aaee1e115d2ab