Anmolkhurana88/Sign-Language-Translator
0
1from google import genai
2from collections import deque
3import time
4
5from dotenv import load_dotenv
6import os
7
8load_dotenv()
9
10client = genai.Client()
11
12# generator = pipeline(task='text-generation', model="meta-llama/Llama-3.2-3B-Instruct")
13
14def generator(prompt, max_new_tokens=50):
15 try:
16 response = client.models.generate_content(
17 model="gemini-2.5-flash",
18 contents=prompt
19 )
20 return response.text
21
22 except Exception as e:
23 print(f"Error in text generation: {e}")
24 return ""
25
26def generate_text(gloss_input, last_text=''):
27 instruct = 'You are a gloss-to-English converter. Output only the sentence using only given gloss tokens. No need to complete it with additional words. No explanations.'
28 prompt = f'{instruct}\nGloss: {gloss_input}\nSentence:'
29
30 max_tokens = len(gloss_input.split()) + len(prompt.split())
31
32 start = time.time()
33 output = generator(prompt, max_new_tokens=max_tokens)
34 # output = gloss_input
35
36 end = time.time()
37 print(f"Time taken for generation: {end - start} seconds")
38
39 if prompt in output:
40 output = output.replace(prompt, '').strip()
41 if '(' in output:
42 output = output.split('(')[0].strip()
43 else:
44 output = output.strip()
45
46 output = output.replace('_', ' ').strip()
47 return output.split('\n')[0]
48
49WINDOW_SIZE = 8
50MIN_TRIGGER = 4
51MAX_CONSUME = 6
52SILENCE_TIMEOUT = 3
53CONF_THRESHOLD = 0.7
54
55# Buffer Manager Class
56class GlossBuffer:
57 def __init__(self):
58 self.buffer = deque(maxlen=WINDOW_SIZE)
59 self.last_gloss_time = 0
60
61 def append_gloss(self, gloss):
62 curr_time = time.time()
63 self.update_buffer(curr_time)
64
65 if gloss not in self.get_buffer():
66 self.buffer.append((gloss, curr_time))
67
68 self.last_gloss_time = curr_time
69
70 def update_buffer(self, curr_time):
71 curr_time = time.time()
72
73 if curr_time - self.last_gloss_time > SILENCE_TIMEOUT:
74 # Clear buffer after prolonged silence
75 self.buffer.clear()
76
77 # Remove old glosses
78 while len(self.buffer) > 0 and self.buffer[0][1] < curr_time - SILENCE_TIMEOUT:
79 self.buffer.popleft()
80
81 def get_buffer(self):
82 return [t for t,c in self.buffer]
83
84 def get_gloss_list(self, counter):
85 gloss_list = self.get_buffer()
86
87 if len(gloss_list) < MIN_TRIGGER and time.time() - counter['last_text_time'] < SILENCE_TIMEOUT:
88 # Not enough glosses to trigger generation and recently generated text
89 return []
90
91 counter['last_text_time'] = time.time()
92
93 # removing older glosses
94 if len(gloss_list) > MAX_CONSUME:
95 gloss_list = gloss_list[:MAX_CONSUME]
96
97 return gloss_list
98
99
100def generate_continue_text(gloss_buffer, text_buffer, counter):
101 gloss_list = gloss_buffer.get_gloss_list(counter)
102
103 if len(gloss_list) > 0:
104 text_list = list(text_buffer)
105
106 gloss_text = ' '.join(gloss_list)
107
108 if len(gloss_list) >= MIN_TRIGGER:
109 gen_text = generate_text(gloss_text, ' '.join(text_list))
110 else:
111 gen_text = gloss_text
112
113 if gen_text and gen_text.strip() != '':
114 text_buffer.extend(gen_text.split())
115 return gen_text
116
117 return ''
118
119def create_text_buffer(max_size=50):
120 return deque(maxlen=max_size)
121
122if __name__ == "__main__":
123 text = "HELLO HOW YOU/YOUR FEEL TODAY I/ME THINK FUTURE CAREER PLAN YOU/YOUR LIKE/LOVE BOOK_READ OR MOVIE/FILM"
124 gloss_buffer = GlossBuffer()
125
126 text_buffer = create_text_buffer()
127
128 for word in text.split():
129 gloss_buffer.append_gloss(word)
130 generate_continue_text(gloss_buffer, text_buffer, {})