File size: 2,974 Bytes
ed7ce33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import json
import time
import requests
from PyQt6.QtCore import QThread, pyqtSignal

class WebImageWorker(QThread):
    zadanie_zakonczone = pyqtSignal(str, str)

    def __init__(self, zapytanie_tekstowe):
        super().__init__()
        self.zapytanie_tekstowe = zapytanie_tekstowe

    def run(self):
        try:
            sciezka_konfiguracji = "C:\\Project_Alt\\1400_vision.json"
            if not os.path.exists(sciezka_konfiguracji):
                self.zadanie_zakonczone.emit("ALT", "Blad: Brak pliku konfiguracyjnego 1400_vision.json")
                return

            with open(sciezka_konfiguracji, "r", encoding="utf-8") as f:
                dane_konfiguracyjne = json.load(f)

            api_token = dane_konfiguracyjne.get("1400", {}).get("1406_api_key", "").strip()
            folder_pobierania = dane_konfiguracyjne.get("1400", {}).get("1402_folder_downloads", "C:\\Project_Alt\\Assets\\Downloads")

            if not api_token:
                self.zadanie_zakonczone.emit("ALT", "Blad: Brak klucza Exa API w konfiguracji")
                return

            from exa_py import Exa
            klient_exa = Exa(api_key=api_token)
            
            wyniki_wyszukiwania = klient_exa.search(
                f"high resolution direct image url of {self.zapytanie_tekstowe}",
                type="auto",
                output_schema={
                    "type": "object",
                    "description": "Image extraction schema",
                    "required": ["image"],
                    "properties": {
                        "image": {
                            "type": "object",
                            "required": ["url"],
                            "properties": {
                                "url": {"type": "string"}
                            }
                        }
                    }
                },
                contents={"contents": {"text": {"maxCharacters": 1000}}}
            )

            if wyniki_wyszukiwania and wyniki_wyszukiwania.output and wyniki_wyszukiwania.output.content:
                zawartosc_content = wyniki_wyszukiwania.output.content
                if isinstance(zawartosc_content, str):
                    dane_wyjsciowe = json.loads(zawartosc_content)
                else:
                    dane_wyjsciowe = zawartosc_content
                
                url_obrazu = dane_wyjsciowe.get("image", {}).get("url", "").strip()
                
                if url_obrazu and ("http://" in url_obrazu or "https://" in url_obrazu):
                    self.zadanie_zakonczone.emit("ALT", url_obrazu)
                    return

            self.zadanie_zakonczone.emit("ALT", f"Nie znaleziono zasobu graficznego dla: {self.zapytanie_tekstowe}")
        except Exception as e:
            self.zadanie_zakonczone.emit("ALT", f"Blad krytyczny rurociagu sieciowego: {str(e)}")