Gunroar commited on
Commit
1b1da2e
·
1 Parent(s): 431d870
Files changed (4) hide show
  1. README.md +114 -12
  2. main.py +1 -1
  3. multilingual/multi.py +41 -98
  4. requirements.txt +1 -1
README.md CHANGED
@@ -1,12 +1,114 @@
1
- ---
2
- title: FARMLINGUAMULTILINGUA
3
- emoji: 🐢
4
- colorFrom: yellow
5
- colorTo: pink
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- short_description: multilingual ability of farmlingua
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Farmlingua Multilingual Translation API
2
+
3
+ A FastAPI-based multilingual translation service supporting Igbo, Hausa, Yoruba, Swahili, and English for agricultural queries.
4
+
5
+ ## Features
6
+
7
+ - **Simple Language Identifier**: Specify language with `lang` parameter
8
+ - **Single Endpoint**: `/ask` - Send text with language identifier
9
+ - **Supported Languages**: English (en), Igbo (ig), Hausa (ha), Yoruba (yo), Swahili (sw)
10
+ - **Agricultural Focus**: Specialized for farming and agricultural queries
11
+ - **External Integration**: Connects to Milestone 1 Q&A service
12
+
13
+ ## Quick Start
14
+
15
+ ### Local Development
16
+
17
+ 1. **Install Dependencies**
18
+ ```bash
19
+ pip install -r requirements.txt
20
+ ```
21
+
22
+ 2. **Run the Server**
23
+ ```bash
24
+ python main.py
25
+ ```
26
+
27
+ 3. **Test the API**
28
+ ```bash
29
+ python test_simple.py
30
+ ```
31
+
32
+ 4. **Access Documentation**
33
+ - Interactive API docs: http://localhost:8000/docs
34
+ - Health check: http://localhost:8000/health
35
+
36
+ ### Docker Deployment
37
+
38
+ 1. **Build Image**
39
+ ```bash
40
+ docker build -t farmlingua-api .
41
+ ```
42
+
43
+ 2. **Run Container**
44
+ ```bash
45
+ docker run -p 8000:8000 farmlingua-api
46
+ ```
47
+
48
+ ## API Usage
49
+
50
+ ### Single Endpoint: `/ask`
51
+
52
+ **POST** `/ask`
53
+
54
+ **Request Body:**
55
+ ```json
56
+ {
57
+ "text": "Kedu ka m ga-esi na-akụ ọka?",
58
+ "lang": "ig"
59
+ }
60
+ ```
61
+
62
+ **Response:**
63
+ ```json
64
+ {
65
+ "language": "Igbo",
66
+ "language_code": "ig",
67
+ "original_text": "Kedu ka m ga-esi na-akụ ọka?",
68
+ "english_translation": "How can I plant grain?",
69
+ "response": "Jiri oghere mkpụrụ osisi ma ọ bụ osisi aka..."
70
+ }
71
+ ```
72
+
73
+ ### Supported Language Codes
74
+
75
+ | Code | Language | NLLB Code |
76
+ |------|----------|-----------|
77
+ | `en` | English | `eng_Latn` |
78
+ | `ig` | Igbo | `ibo_Latn` |
79
+ | `ha` | Hausa | `hau_Latn` |
80
+ | `yo` | Yoruba | `yor_Latn` |
81
+ | `sw` | Swahili | `swh_Latn` |
82
+
83
+ ## Project Structure
84
+
85
+ ```
86
+ FARMLINGUAMULTILINGUA/
87
+ ├── models/
88
+ │ └── model.py # Hugging Face translation model
89
+ ├── multilingual/
90
+ │ └── multi.py # FastAPI application
91
+ ├── main.py # Application entry point
92
+ ├── test_simple.py # Test suite
93
+ ├── requirements.txt # Python dependencies
94
+ ├── Dockerfile # Docker configuration
95
+ ├── .gitignore # Git ignore rules
96
+ └── README.md # This file
97
+ ```
98
+
99
+ ## Environment Variables
100
+
101
+ - `HOST`: Server host (default: `0.0.0.0`)
102
+ - `PORT`: Server port (default: `8000`)
103
+
104
+ ## Technology Stack
105
+
106
+ - **FastAPI**: Web framework
107
+ - **Transformers**: Hugging Face model pipeline
108
+ - **NLLB Model**: `drrobot9/nllb-ig-yo-ha-finetuned`
109
+ - **Uvicorn**: ASGI server
110
+ - **Docker**: Containerization
111
+
112
+ ## License
113
+
114
+ MIT License
main.py CHANGED
@@ -4,7 +4,7 @@ import uvicorn
4
 
5
  if __name__ == "__main__":
6
  host = os.getenv("HOST", "0.0.0.0")
7
- port = int(os.getenv("PORT", "7860"))
8
  uvicorn.run("multilingual.multi:app", host=host, port=port, reload=False)
9
 
10
 
 
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
 
multilingual/multi.py CHANGED
@@ -9,6 +9,7 @@ import requests
9
  import os
10
  from models.model import translator
11
 
 
12
  nest_asyncio.apply()
13
 
14
  lang_codes = {
@@ -19,6 +20,8 @@ lang_codes = {
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",
@@ -27,7 +30,7 @@ app = FastAPI(
27
 
28
  class Query(BaseModel):
29
  text: str
30
- lang: str
31
 
32
  def translate(text: str, src: str, tgt: str) -> str:
33
  try:
@@ -57,13 +60,18 @@ 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
  }
@@ -73,112 +81,47 @@ 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__":
 
9
  import os
10
  from models.model import translator
11
 
12
+
13
  nest_asyncio.apply()
14
 
15
  lang_codes = {
 
20
  "sw": "swh_Latn"
21
  }
22
 
23
+ # Simple language identifier - no auto-detection needed
24
+
25
  app = FastAPI(
26
  title="Farmlingua Translation API",
27
  description="Multilingual ability for farmlingua supporting Igbo, Hausa, Yoruba, and Swahili",
 
30
 
31
  class Query(BaseModel):
32
  text: str
33
+ lang: str
34
 
35
  def translate(text: str, src: str, tgt: str) -> str:
36
  try:
 
60
  """Root endpoint with API information"""
61
  return {
62
  "message": "Welcome to Farmlingua Translation API",
63
+ "version": "3.0",
64
+ "features": {
65
+ "simple_language_identifier": "Specify language with 'lang' parameter",
66
+ "single_endpoint": "/ask - Send text with language identifier",
67
+ "supported_codes": "en, ig, ha, yo, sw"
68
+ },
69
  "supported_languages": {
70
+ "en": "English",
71
+ "ig": "Igbo",
72
+ "ha": "Hausa",
73
+ "yo": "Yoruba",
74
+ "sw": "Swahili"
75
  },
76
  "documentation": "/docs"
77
  }
 
81
  """Health check endpoint"""
82
  return {"status": "healthy", "model_loaded": translator is not None}
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  @app.post("/ask")
85
  def multilingual_ask(query: Query):
86
+ """Multilingual endpoint with simple language identifier"""
87
  user_text = query.text
88
  user_lang = query.lang.lower()
89
+
90
+ # Validate language code
91
  if user_lang not in lang_codes:
92
  return {"error": "Unsupported language. Use one of: en, ig, ha, yo, sw."}
93
+
94
+ # Get language name for response
95
+ lang_names = {
96
+ "en": "English",
97
+ "ig": "Igbo",
98
+ "ha": "Hausa",
99
+ "yo": "Yoruba",
100
+ "sw": "Swahili"
101
+ }
102
+
103
+ # Translate to English if not English
104
  if user_lang != "en":
105
  english_text = translate(user_text, user_lang, "en")
106
  else:
107
  english_text = user_text
108
+
109
+ # Get response from Milestone 1
110
  english_response = query_milestone1(english_text)
111
+
112
+ # Translate response back to original language if not English
113
  if user_lang != "en":
114
  final_response = translate(english_response, "en", user_lang)
115
  else:
116
  final_response = english_response
117
+
118
+ return {
119
+ "language": lang_names[user_lang],
120
+ "language_code": user_lang,
121
+ "original_text": user_text,
122
+ "english_translation": english_text,
123
+ "response": final_response
124
+ }
125
 
126
  # Start ngrok tunnel and run FastAPI server
127
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -5,4 +5,4 @@ requests
5
  transformers
6
  torch
7
  nest-asyncio
8
- pyngrok
 
5
  transformers
6
  torch
7
  nest-asyncio
8
+ pyngrok