waqas0707 commited on
Commit
da747eb
·
verified ·
1 Parent(s): eb6d342

Rename app.py to main.py

Browse files
Files changed (2) hide show
  1. app.py +0 -64
  2. main.py +74 -0
app.py DELETED
@@ -1,64 +0,0 @@
1
- from fastapi import FastAPI, Request
2
- from pydantic import BaseModel
3
- import numpy as np
4
- import tensorflow as tf
5
- import json
6
- import os
7
-
8
- app = FastAPI()
9
-
10
- # Load model files
11
- encoder_model = tf.keras.models.load_model("saved_models/encoder.h5")
12
- decoder_model = tf.keras.models.load_model("saved_models/decoder.h5")
13
-
14
- # Load token files
15
- with open("saved_models/input_token_index.json", "r") as f:
16
- input_token_index = json.load(f)
17
-
18
- with open("saved_models/target_token_index.json", "r") as f:
19
- target_token_index = json.load(f)
20
-
21
- with open("saved_models/reverse_target_char_index.json", "r") as f:
22
- reverse_target_char_index = json.load(f)
23
-
24
- max_encoder_seq_length = 20 # adjust based on your training
25
- num_decoder_tokens = len(target_token_index)
26
-
27
- class InputText(BaseModel):
28
- text: str
29
-
30
- def decode_sequence(input_seq):
31
- # Convert input text to one-hot
32
- encoder_input_data = np.zeros((1, max_encoder_seq_length, len(input_token_index)))
33
- for t, char in enumerate(input_seq):
34
- if char in input_token_index:
35
- encoder_input_data[0, t, input_token_index[char]] = 1.0
36
-
37
- states_value = encoder_model.predict(encoder_input_data)
38
-
39
- target_seq = np.zeros((1, 1, num_decoder_tokens))
40
- target_seq[0, 0, target_token_index["\t"]] = 1.0
41
-
42
- stop_condition = False
43
- decoded_sentence = ""
44
- while not stop_condition:
45
- output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
46
-
47
- sampled_token_index = np.argmax(output_tokens[0, -1, :])
48
- sampled_char = reverse_target_char_index[str(sampled_token_index)]
49
- decoded_sentence += sampled_char
50
-
51
- if sampled_char == "\n" or len(decoded_sentence) > max_encoder_seq_length:
52
- stop_condition = True
53
-
54
- target_seq = np.zeros((1, 1, num_decoder_tokens))
55
- target_seq[0, 0, sampled_token_index] = 1.0
56
- states_value = [h, c]
57
-
58
- return decoded_sentence.strip()
59
-
60
- @app.post("/translate")
61
- def translate_text(input: InputText):
62
- roman_urdu = input.text.lower()
63
- result = decode_sequence(roman_urdu)
64
- return {"result": result}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from pydantic import BaseModel
3
+ from tensorflow.keras.models import load_model
4
+ import numpy as np
5
+ import pickle
6
+
7
+ # Initialize FastAPI app
8
+ app = FastAPI(title="Roman Urdu to Urdu Translator API")
9
+
10
+ # Load models
11
+ encoder_model = load_model('roman_to_urdu_encoder.keras')
12
+ decoder_model = load_model('roman_to_urdu_decoder.keras')
13
+
14
+ # Load token indices and dictionary
15
+ with open('roman_urdu_dict.pkl', 'rb') as f:
16
+ translation_dict = pickle.load(f)
17
+
18
+ with open('roman_urdu_indices.pkl', 'rb') as f:
19
+ data = pickle.load(f)
20
+
21
+ input_token_index = data['input_token_index']
22
+ target_token_index = data['target_token_index']
23
+ reverse_target_char_index = data['reverse_target_char_index']
24
+ max_encoder_seq_length = data['max_encoder_seq_length']
25
+ max_decoder_seq_length = data['max_decoder_seq_length']
26
+
27
+ # Input schema
28
+ class TranslateRequest(BaseModel):
29
+ text: str
30
+
31
+ # Decoder function
32
+ def decode_sequence(input_seq):
33
+ states_value = encoder_model.predict(input_seq)
34
+ target_seq = np.zeros((1, 1, len(target_token_index)))
35
+ target_seq[0, 0, target_token_index['\t']] = 1.0
36
+
37
+ decoded_sentence = ""
38
+ while True:
39
+ output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
40
+ sampled_token_index = np.argmax(output_tokens[0, -1, :])
41
+ sampled_char = reverse_target_char_index[sampled_token_index]
42
+ decoded_sentence += sampled_char
43
+
44
+ if sampled_char == "\n" or len(decoded_sentence) > max_decoder_seq_length:
45
+ break
46
+
47
+ target_seq = np.zeros((1, 1, len(target_token_index)))
48
+ target_seq[0, 0, sampled_token_index] = 1.0
49
+ states_value = [h, c]
50
+
51
+ return decoded_sentence.strip()
52
+
53
+ # Translate single word
54
+ def translate_single_word(word):
55
+ input_seq = np.zeros((1, max_encoder_seq_length, len(input_token_index)))
56
+ for t, char in enumerate(word):
57
+ if char in input_token_index:
58
+ input_seq[0, t, input_token_index[char]] = 1.0
59
+ return decode_sequence(input_seq)
60
+
61
+ # Main translation function
62
+ def translate_to_urdu(roman_input: str):
63
+ if roman_input in translation_dict:
64
+ return translation_dict[roman_input]
65
+ if ' ' in roman_input:
66
+ return ' '.join([translation_dict.get(word, translate_single_word(word)) for word in roman_input.split()])
67
+ return translate_single_word(roman_input)
68
+
69
+ # API route
70
+ @app.post("/translate")
71
+ async def translate(request: TranslateRequest):
72
+ roman_text = request.text.strip()
73
+ urdu_output = translate_to_urdu(roman_text)
74
+ return {"urdu_translation": urdu_output}