Gunroar commited on
Commit
a7c894d
·
1 Parent(s): 5bd8e52

commit application

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. Dockerfile +34 -0
  3. main.py +10 -0
  4. models/model.py +12 -0
  5. multilingual/multi.py +187 -0
  6. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # syntax=docker/dockerfile:1
2
+
3
+ FROM python:3.10-slim
4
+
5
+ ENV PYTHONDONTWRITEBYTECODE=1 \
6
+ PYTHONUNBUFFERED=1 \
7
+ PIP_NO_CACHE_DIR=1 \
8
+ HF_HOME=/root/.cache/huggingface \
9
+ TRANSFORMERS_CACHE=/root/.cache/huggingface/transformers \
10
+ TOKENIZERS_PARALLELISM=false
11
+
12
+ WORKDIR /app
13
+
14
+ # System deps (for scientific libs and uvicorn)
15
+ RUN apt-get update && apt-get install -y --no-install-recommends \
16
+ build-essential \
17
+ git \
18
+ && rm -rf /var/lib/apt/lists/*
19
+
20
+ # Copy only requirements first for better layer caching
21
+ COPY requirements.txt ./
22
+
23
+ RUN pip install --upgrade pip && \
24
+ pip install -r requirements.txt
25
+
26
+ # Copy the rest of the application
27
+ COPY . .
28
+
29
+ EXPOSE 8000
30
+
31
+ # Default command: run the main.py runner
32
+ CMD ["python", "main.py"]
33
+
34
+
main.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uvicorn
3
+
4
+
5
+ if __name__ == "__main__":
6
+ host = os.getenv("HOST", "0.0.0.0")
7
+ port = int(os.getenv("PORT", "8000"))
8
+ uvicorn.run("multilingual.multi:app", host=host, port=port, reload=False)
9
+
10
+
models/model.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## loading translation model
2
+
3
+ from transformers import pipeline
4
+
5
+ model_id = "drrobot9/nllb-ig-yo-ha-finetuned"
6
+
7
+ translator = pipeline("translation", model=model_id, device=-1)
8
+
9
+ if __name__ == "__main__":
10
+ test_text = "Kedu ihe ị na-eme?"
11
+ translation = translator(test_text, src_lang="ibo_Latn", tgt_lang="eng_Latn")
12
+ print("Test Translation (Igbo -> English):", translation[0]['translation_text'])
multilingual/multi.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## translation layer for Farmlingua (Milestone2)
2
+
3
+ import nest_asyncio
4
+ from pyngrok import ngrok
5
+ import uvicorn
6
+ from fastapi import FastAPI
7
+ from pydantic import BaseModel
8
+ import requests
9
+ import os
10
+ from models.model import translator
11
+
12
+ nest_asyncio.apply()
13
+
14
+ lang_codes = {
15
+ "en": "eng_Latn",
16
+ "ig": "ibo_Latn",
17
+ "ha": "hau_Latn",
18
+ "yo": "yor_Latn",
19
+ "sw": "swh_Latn"
20
+ }
21
+
22
+ app = FastAPI(
23
+ title="Farmlingua Translation API",
24
+ description="Multilingual ability for farmlingua supporting Igbo, Hausa, Yoruba, and Swahili",
25
+ version="0.0.1"
26
+ )
27
+
28
+ class Query(BaseModel):
29
+ text: str
30
+ lang: str
31
+
32
+ def translate(text: str, src: str, tgt: str) -> str:
33
+ try:
34
+ out = translator(text, src_lang=lang_codes[src], tgt_lang=lang_codes[tgt])
35
+ return out[0]["translation_text"]
36
+ except Exception as e:
37
+ return f"Translation error: {str(e)}"
38
+
39
+ def query_milestone1(prompt: str) -> str:
40
+ try:
41
+ response = requests.post(
42
+ "https://remostart-milestone-one-farmlingua-ai.hf.space/ask",
43
+ json={"query": prompt},
44
+ timeout=200
45
+ )
46
+ if response.status_code == 200:
47
+ data = response.json()
48
+ return data.get("answer") or str(data)
49
+ else:
50
+ return f"Base model error ({response.status_code}): {response.text}"
51
+ except Exception as e:
52
+ return f"Failed to connect to Milestone 1: {str(e)}"
53
+
54
+ # Root endpoint
55
+ @app.get("/")
56
+ def root():
57
+ """Root endpoint with API information"""
58
+ return {
59
+ "message": "Welcome to Farmlingua Translation API",
60
+ "version": "2.0",
61
+ "supported_languages": {
62
+ "igbo": "/igbo",
63
+ "hausa": "/hausa",
64
+ "yoruba": "/yoruba",
65
+ "swahili": "/swahili",
66
+ "multilingual": "/ask"
67
+ },
68
+ "documentation": "/docs"
69
+ }
70
+
71
+ @app.get("/health")
72
+ def health_check():
73
+ """Health check endpoint"""
74
+ return {"status": "healthy", "model_loaded": translator is not None}
75
+
76
+ # endpoints for each language
77
+ @app.post("/igbo")
78
+ def igbo_endpoint(query: Query):
79
+ """Endpoint specifically for Igbo language queries"""
80
+ user_text = query.text
81
+
82
+ # Translate Igbo to English
83
+ english_text = translate(user_text, "ig", "en")
84
+
85
+ # Get response from Milestone 1
86
+ english_response = query_milestone1(english_text)
87
+
88
+ # Translate response back to Igbo
89
+ igbo_response = translate(english_response, "en", "ig")
90
+
91
+ return {
92
+ "language": "Igbo",
93
+ "original_text": user_text,
94
+ "english_translation": english_text,
95
+ "response": igbo_response
96
+ }
97
+
98
+ @app.post("/hausa")
99
+ def hausa_endpoint(query: Query):
100
+ """Endpoint specifically for Hausa language queries"""
101
+ user_text = query.text
102
+
103
+ # Translate Hausa to English
104
+ english_text = translate(user_text, "ha", "en")
105
+
106
+ # Get response from Milestone 1
107
+ english_response = query_milestone1(english_text)
108
+
109
+ # Translate response back to Hausa
110
+ hausa_response = translate(english_response, "en", "ha")
111
+
112
+ return {
113
+ "language": "Hausa",
114
+ "original_text": user_text,
115
+ "english_translation": english_text,
116
+ "response": hausa_response
117
+ }
118
+
119
+ @app.post("/yoruba")
120
+ def yoruba_endpoint(query: Query):
121
+ """Endpoint specifically for Yoruba language queries"""
122
+ user_text = query.text
123
+
124
+ # Translate Yoruba to English
125
+ english_text = translate(user_text, "yo", "en")
126
+
127
+ # Get response from Milestone 1
128
+ english_response = query_milestone1(english_text)
129
+
130
+ # Translate response back to Yoruba
131
+ yoruba_response = translate(english_response, "en", "yo")
132
+
133
+ return {
134
+ "language": "Yoruba",
135
+ "original_text": user_text,
136
+ "english_translation": english_text,
137
+ "response": yoruba_response
138
+ }
139
+
140
+ @app.post("/swahili")
141
+ def swahili_endpoint(query: Query):
142
+ """Endpoint specifically for Swahili language queries"""
143
+ user_text = query.text
144
+
145
+ # Translate Swahili to English
146
+ english_text = translate(user_text, "sw", "en")
147
+
148
+ # Get response from Milestone 1
149
+ english_response = query_milestone1(english_text)
150
+
151
+ # Translate response back to Swahili
152
+ swahili_response = translate(english_response, "en", "sw")
153
+
154
+ return {
155
+ "language": "Swahili",
156
+ "original_text": user_text,
157
+ "english_translation": english_text,
158
+ "response": swahili_response
159
+ }
160
+
161
+ @app.post("/ask")
162
+ def multilingual_ask(query: Query):
163
+ user_text = query.text
164
+ user_lang = query.lang.lower()
165
+
166
+ if user_lang not in lang_codes:
167
+ return {"error": "Unsupported language. Use one of: en, ig, ha, yo, sw."}
168
+
169
+ if user_lang != "en":
170
+ english_text = translate(user_text, user_lang, "en")
171
+ else:
172
+ english_text = user_text
173
+
174
+ english_response = query_milestone1(english_text)
175
+
176
+ if user_lang != "en":
177
+ final_response = translate(english_response, "en", user_lang)
178
+ else:
179
+ final_response = english_response
180
+
181
+ return {"response": final_response}
182
+
183
+ # Start ngrok tunnel and run FastAPI server
184
+ if __name__ == "__main__":
185
+ public_url = ngrok.connect(8000)
186
+ print(f"Public URL: {public_url}")
187
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pydantic
4
+ requests
5
+ transformers
6
+ torch
7
+ nest-asyncio
8
+ pyngrok