-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (154 loc) · 4.58 KB
/
main.py
File metadata and controls
183 lines (154 loc) · 4.58 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# main.py
import openai
from scraper import extrage_text_lege as ext, test_structura_juridica as test
from search_google import cauta_lege_google, cauta_lege_multiple_engines as cauta_all, cauta_fallback
try:
from multi_site_extractor import MultiSiteLegalExtractor as Extractor
MULTI = True
except ImportError:
MULTI = False
print("multi-site extractor lipsa")
openai.api_base = "http://localhost:11434/v1"
openai.api_key = "------------"
def ask_llm(ctx, q):
try:
cuv = ['garantie', 'produs', 'cumparator', 'consumator', 'firma', 'magazin', 'defect', 'stricat']
e_consumator = any(c in q.lower() for c in cuv)
if e_consumator:
sys = "Esti expert in dreptul consumatorilor. Raspunde pe baza legii."
else:
sys = "Esti asistent juridic. Raspunde doar pe baza textului."
r = openai.ChatCompletion.create(
model="mistral",
messages=[
{"role": "system", "content": sys},
{"role": "user", "content": f"{ctx}\n\nINTREBARE: {q}"}
],
temperature=0.1,
max_tokens=2500
)
return r['choices'][0]['message']['content']
except Exception as e:
return f"Eroare: {e}"
def banner():
print("=" * 40)
print("LEGALIZE AI")
print("=" * 40)
def meniu():
print("\nOptiuni:")
print("1. Intrebare")
print("2. Cautare multipla")
print("3. Testeaza URL")
print("4. Analiza structura")
print("5. Site-uri")
print("6. Iesire")
def intrebare():
q = input("Intrebarea:\n> ").strip()
if not q:
print("Intrebare invalida")
return
print("Caut lege...")
url = cauta_fallback(q)
if not url.startswith("http"):
print(url)
return
print("Extrag text...")
txt = ext(url)
if txt.startswith("Eroare") or not txt:
print("Nu s-a extras nimic")
return
s = test(txt)
if s['articole'] == 0:
print("Structura slaba. Incerc altceva...")
rez = cauta_all(q)
if len(rez) > 1:
txt2 = ext(rez[1]['url'])
if not txt2.startswith("Eroare"):
txt = txt2
url = rez[1]['url']
ans = ask_llm(txt, q)
print("\nRaspuns:")
print(ans)
print("\nSursa:", url)
def cautare_multipla():
if not MULTI:
print("Modul lipsa")
return
q = input("Intrebarea:\n> ").strip()
if not q:
print("Intrebare invalida")
return
rez = cauta_all(q)
if not rez:
print("Nimic gasit")
return
for i, r in enumerate(rez[:5], 1):
print(f"{i}. {r['site']} - {r['url'][:80]}")
try:
i = int(input("Alege: ")) - 1
if 0 <= i < len(rez):
url = rez[i]['url']
url_direct(url)
except:
print("Input invalid")
def url_direct(url=None):
if not url:
url = input("URL:\n> ").strip()
if not url.startswith("http"):
print("URL invalid")
return
txt = ext(url)
if txt.startswith("Eroare"):
print("Eroare la extragere")
return
rasp = input("Pui intrebare? (da/nu): ").strip().lower()
if rasp in ['da', 'd', 'y']:
q = input("Intrebarea: ").strip()
if q:
ans = ask_llm(txt, q)
print("\nRaspuns:")
print(ans)
print("\nSursa:", url)
def siteuri():
print("Site-uri suportate:")
print("- legislatie.just.ro")
print("- lege5.ro")
print("- anpc.ro")
print("- cdep.ro")
def test_structura():
url = input("URL:\n> ").strip()
if not url.startswith("http"):
print("URL invalid")
return
txt = ext(url)
if txt.startswith("Eroare"):
print("Eroare la extragere")
return
print("Text:")
print(txt[:300])
s = test(txt)
print("Articole:", s['articole'])
print("Alineate:", s['alineate'])
print("Litere:", s['litere'])
def main():
banner()
while True:
meniu()
opt = input("Optiune: ").strip()
if opt == "1":
intrebare()
elif opt == "2":
cautare_multipla()
elif opt == "3":
url_direct()
elif opt == "4":
test_structura()
elif opt == "5":
siteuri()
elif opt == "6":
print("Bye")
break
else:
print("Optiune invalida")
if __name__ == "__main__":
main()