-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite.py
114 lines (85 loc) · 3.83 KB
/
website.py
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
import sys
import os
import json
from flask import Flask, request
from JSONizer import *
from LLMInterface import FakeGenerator, ALLAM_GENERATOR
from ShatrGenerator import ShatrGenerator
from Prompter import Prompter, format_abyat
from Critic import CriticGen, CriticChat
from Analyzer import Analyzer
from main import generate_qasida, load_env
from Data import load_poets, load_bohours
# Initialize The Flask App
app = Flask(__name__)
#app.secret_key = 'your_secret_key'
#app.config['SESSION_TYPE'] = 'filesystem' # Use server-side session
# Initialize heavy components
prompter = Prompter()
analyzer = Analyzer()
load_env(".env")
#llm = FakeGenerator()
api_key = os.environ.get("API_KEY")
llm = ALLAM_GENERATOR(api_key)
critic = CriticGen(llm=llm)
shatr_generator = ShatrGenerator(llm, prompter=prompter, analyzer=analyzer)
@app.route('/generate', methods=['GET'])
def generate():
JSONizer.resetResponse()
user_input = request.form['prompt']
selected_bahr = request.form['bahr']
selected_poet = request.form['poet']
prompter.update(None, selected_poet, selected_bahr)
len_ = 3
output = generate_qasida(user_input, shatr_generator, critic, selected_bahr, None, len_ )
return JSONizer.getGenResponse()
@app.route('/analyze', methods=['GET'])
def analyze():
JSONizer.resetResponse()
shatrs = json.loads(request.form['shatrs'])
print(shatrs)
abyat = format_abyat(shatrs)
expected_wazn = None
for b in range(0, len(shatrs), 2): #for each bayt
prev_shatrs = shatrs[:b]
s_ = shatrs[b: b+2]
#TODO: move this to critic full
#copied from generate_qasida
_, new_wazn_name1, new_wazn_combs1, new_wazn_mismatch1, diacritized1, arudi_indices1, tf3elat1, aroodi_writing1 = analyzer.extract(s_[0], expected_wazn_name=expected_wazn)
expected_wazn = new_wazn_name1
new_qafiya, new_wazn_name2, new_wazn_combs2, new_wazn_mismatch2, diacritized2, arudi_indices2, tf3elat2, aroodi_writing2 = analyzer.extract(s_[1], expected_wazn_name=expected_wazn)
hardcoded_feedback = ""
#TODO Hardcoded Qafiya + Wazn feedbacks!
feedback = critic.critic(s_, prev_shatrs, None, hardcoded_feedback)
text_mis1 = arudi_indices1[new_wazn_mismatch1]
text_mis2 = arudi_indices2[new_wazn_mismatch2]
if new_wazn_mismatch1 == -1:
text_mis1 = -1
if new_wazn_mismatch2 == -1:
text_mis2 = -1
#JSONizer.analysis(diacritized1, new_wazn_combs1, new_wazn_mismatch1, feedback["feedback"], text_mis1, tf3elat1)
JSONizer.nextShatr()
JSONizer.attempt(diacritized1, aroodi_writing1, new_wazn_combs1, new_wazn_mismatch1, diacritized1, text_mis1, tf3elat1, new_wazn_name1, feedback=feedback["feedback"])
JSONizer.nextShatr()
JSONizer.attempt(diacritized2, aroodi_writing2, new_wazn_combs2, new_wazn_mismatch2, diacritized2, text_mis2, tf3elat2, new_wazn_name2, feedback=feedback["feedback"])
#JSONizer.analysis(diacritized2, new_wazn_combs2, new_wazn_mismatch2, feedback["feedback"], text_mis2, tf3elat2)
return JSONizer.getAnalyzerResponse()
if __name__ == '__main__':
app.testing = True
#app.run(debug=True)
c = app.test_client()
resp = c.get('/generate', data={
"type":"generate",
"prompt":"اكتب قصيدة عن الوطن",
"bahr":"الطويل",
"poet":"علام",
})
print(resp.data.decode())
print("---------")
resp = c.get('/analyze', data={
"type":"analyze",
"shatrs":json.dumps([
'حييت يا وطني الحبيب تحيةً', 'ﻓنـحيب قلبي والضلوع تحومل','يَا سَامِعِي أَصْوَاتِ أَبْيَاتِي الْغَرَّ', 'يَامَنْ بِلُبّي تُعَدُّوْنَ وَتُسْتَهَلْ'
])
})
print(resp.data.decode())