Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,101 @@
|
|
1 |
-
from
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
def
|
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 |
-
output += response.token.text
|
41 |
-
yield output
|
42 |
return output
|
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 |
-
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
2 |
+
import torch
|
3 |
+
import pickle
|
4 |
+
import streamlit as st
|
5 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
6 |
+
|
7 |
+
from translate import Translator
|
8 |
+
|
9 |
+
def init_session_state():
|
10 |
+
if 'history' not in st.session_state:
|
11 |
+
st.session_state.history = ""
|
12 |
+
|
13 |
+
# Initialize session state
|
14 |
+
init_session_state()
|
15 |
+
|
16 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-base")
|
17 |
+
# pipe = pipeline("text-generation", model="GeneZC/MiniChat-1.5-3B")
|
18 |
+
# pipe = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2")
|
19 |
+
# model_name = "MoritzLaurer/mDeBERTa-v3-base-mnli-xnli"
|
20 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
21 |
+
# model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
22 |
+
|
23 |
+
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
|
24 |
+
|
25 |
+
# with open('chapter_titles.pkl', 'rb') as file:
|
26 |
+
# titles_astiko = pickle.load(file)
|
27 |
+
# labels1 = ["κληρονομικό", "ακίνητα", "διαζύγιο"]
|
28 |
+
# # labels2 = ["αποδοχή κληρονομιάς", "αποποίηση", "διαθήκη"]
|
29 |
+
# # labels3 = ["μίσθωση", "κυριότητα", "έξωση", "απλήρωτα νοίκια"]
|
30 |
+
|
31 |
+
|
32 |
+
# titles_astiko = ["γάμος", "αλλοδαπός", "φορολογία", "κληρονομικά", "στέγη", "οικογενειακό", "εμπορικό","κλοπή","απάτη"]
|
33 |
+
# Load dictionary from the file using pickle
|
34 |
+
with open('my_dict.pickle', 'rb') as file:
|
35 |
+
dictionary = pickle.load(file)
|
36 |
+
|
37 |
+
def classify(text,labels):
|
38 |
+
output = classifier(text, labels, multi_label=False)
|
39 |
+
|
|
|
|
|
40 |
return output
|
41 |
|
42 |
|
43 |
+
text = st.text_input('Enter some text:') # Input field for new text
|
44 |
+
|
45 |
+
if text:
|
46 |
+
|
47 |
+
labels = list(dictionary)
|
48 |
+
|
49 |
+
output = classify(text,labels)
|
50 |
+
|
51 |
+
output = output["labels"][0]
|
52 |
+
|
53 |
+
labels = list(dictionary[output])
|
54 |
+
|
55 |
+
output2 = classify(text,labels)
|
56 |
+
|
57 |
+
output2 = output2["labels"][0]
|
58 |
+
|
59 |
+
|
60 |
+
answer = dictionary[output][output2]
|
61 |
+
|
62 |
+
# Create a translator object with specified source and target languages
|
63 |
+
translator = Translator(from_lang='el', to_lang='en')
|
64 |
+
translator2 = Translator(from_lang='en', to_lang='el')
|
65 |
+
|
66 |
+
st.text("H ερώτηση σας σχετίζεται με " + output+ " δίκαιο")
|
67 |
+
|
68 |
+
|
69 |
+
# Translate the text from Greek to English
|
70 |
+
answer = translator.translate(answer)
|
71 |
+
text = translator.translate(text)
|
72 |
+
|
73 |
+
st.text("Πιο συγκεκριμένα σχετίζεται με " + output2)
|
74 |
+
|
75 |
+
|
76 |
+
# text_to_translate2 = text[499:999]
|
77 |
+
# translated_text2 = translator.translate(text_to_translate2)
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
st.session_state.history += "Based on this info only:" + answer +" ,answer this question, by reasoning step by step:" + text # Add new text to history
|
82 |
+
out = pipe(st.session_state.history, max_new_tokens=256) # Generate output based on history
|
83 |
+
|
84 |
+
|
85 |
+
# st.text(st.session_state.history)
|
86 |
+
|
87 |
+
translated_text2 = translator2.translate(out[0]['generated_text'])
|
88 |
+
|
89 |
+
|
90 |
+
st.text(translated_text2)
|
91 |
+
|
92 |
+
# with st.expander("View Full Output", expanded=False):
|
93 |
+
# st.write(translated_text2, allow_output_mutation=True)
|
94 |
+
|
95 |
+
# st.text(translated_text2)
|
96 |
+
# st.text("History: " + st.session_state.history)
|
97 |
+
|
98 |
+
# st.text(output)
|
99 |
+
# st.text(output2)
|
100 |
+
|
101 |
+
# st.text(answer)
|