Spaces:
Sleeping
Sleeping
Gabriel Okiri
commited on
Commit
·
2252f9f
1
Parent(s):
e98ff45
test
Browse files- app.py +55 -55
- configs/__init__.py +0 -0
app.py
CHANGED
@@ -1,69 +1,69 @@
|
|
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 |
-
import sys
|
55 |
-
import os
|
56 |
-
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
57 |
|
58 |
-
from model.config import ModelConfig
|
59 |
-
from model.model import NigerianLanguageModel
|
60 |
-
from interface.gradio_app import create_interface
|
61 |
|
62 |
-
def main():
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
if __name__ == "__main__":
|
69 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
# Model initialization
|
6 |
+
model_name = "gpt2" # or your specific model
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Add special tokens for languages
|
11 |
+
special_tokens = {"additional_special_tokens": ["[YORUBA]", "[IGBO]", "[HAUSA]"]}
|
12 |
+
tokenizer.add_special_tokens(special_tokens)
|
13 |
+
model.resize_token_embeddings(len(tokenizer))
|
14 |
|
15 |
+
# Move model to GPU if available
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
model.to(device)
|
18 |
|
19 |
+
def generate_text(prompt, language):
|
20 |
+
# Add language tag to prompt
|
21 |
+
tagged_prompt = f"[{language.upper()}] {prompt}"
|
22 |
|
23 |
+
# Tokenize
|
24 |
+
inputs = tokenizer(tagged_prompt, return_tensors="pt").to(device)
|
25 |
|
26 |
+
# Generate
|
27 |
+
outputs = model.generate(
|
28 |
+
**inputs,
|
29 |
+
max_length=100,
|
30 |
+
num_return_sequences=1,
|
31 |
+
temperature=0.7,
|
32 |
+
pad_token_id=tokenizer.eos_token_id
|
33 |
+
)
|
34 |
|
35 |
+
# Decode and return
|
36 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
|
38 |
+
# Create Gradio interface
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=generate_text,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(label="Enter your prompt"),
|
43 |
+
gr.Dropdown(choices=["YORUBA", "IGBO", "HAUSA"], label="Select Language")
|
44 |
+
],
|
45 |
+
outputs=gr.Textbox(label="Generated Text"),
|
46 |
+
title="Nigerian Language Generator",
|
47 |
+
description="Generate text in Yoruba, Igbo, or Hausa using a fine-tuned GPT model."
|
48 |
+
)
|
49 |
|
50 |
+
if __name__ == "__main__":
|
51 |
+
iface.launch()
|
52 |
|
53 |
|
54 |
+
# import sys
|
55 |
+
# import os
|
56 |
+
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
57 |
|
58 |
+
# from model.config import ModelConfig
|
59 |
+
# from model.model import NigerianLanguageModel
|
60 |
+
# from interface.gradio_app import create_interface
|
61 |
|
62 |
+
# def main():
|
63 |
+
# config = ModelConfig()
|
64 |
+
# model = NigerianLanguageModel(config)
|
65 |
+
# interface = create_interface(model)
|
66 |
+
# interface.launch()
|
67 |
|
68 |
+
# if __name__ == "__main__":
|
69 |
+
# main()
|
configs/__init__.py
ADDED
File without changes
|