Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ api = HfApi()
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
modelName = "MitchellKil/gaelic-ipa-byt5" # my fine tuned model
|
| 15 |
-
|
| 16 |
|
| 17 |
HF_TOKEN = os.environ["HF_TOKEN"] # set in secrets
|
| 18 |
|
|
@@ -27,11 +27,11 @@ model.eval()
|
|
| 27 |
|
| 28 |
print("Model loaded")
|
| 29 |
|
| 30 |
-
class
|
| 31 |
text: str
|
| 32 |
|
| 33 |
|
| 34 |
-
def
|
| 35 |
# Input text - > target text for trained model
|
| 36 |
prompt = f"convert Gaelic to IPA: {text}"
|
| 37 |
|
|
@@ -52,6 +52,11 @@ def text_to_ipa(text: str) -> str:
|
|
| 52 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
return decoded.strip()
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
class Feedback(BaseModel):
|
| 56 |
type: str
|
| 57 |
feedback: str
|
|
@@ -59,14 +64,10 @@ class Feedback(BaseModel):
|
|
| 59 |
translation: str
|
| 60 |
phonetic: str
|
| 61 |
|
| 62 |
-
@app.post("/predict") # call to actually run and return text to project
|
| 63 |
-
def predict(request: TextRequest):
|
| 64 |
-
ipa_result = text_to_ipa(request.text)
|
| 65 |
-
return {"ipa": ipa_result}
|
| 66 |
|
| 67 |
@app.post("/feedback")
|
| 68 |
def save_feedback(data: Feedback):
|
| 69 |
-
|
| 70 |
"type": data.type,
|
| 71 |
"timestamp": datetime.utcnow().isoformat(),
|
| 72 |
"input_text": data.input_text,
|
|
@@ -76,26 +77,27 @@ def save_feedback(data: Feedback):
|
|
| 76 |
}
|
| 77 |
try: # gets current feedback file
|
| 78 |
local_file = hf_hub_download(
|
| 79 |
-
repo_id=
|
| 80 |
filename="feedback.jsonl",
|
| 81 |
repo_type="dataset",
|
| 82 |
token=HF_TOKEN
|
| 83 |
)
|
| 84 |
with open(local_file, "r", encoding="utf-8") as f:
|
| 85 |
lines = f.readlines()
|
| 86 |
-
|
|
|
|
| 87 |
|
| 88 |
-
lines.append(json.dumps(
|
| 89 |
|
| 90 |
-
temp_path = "
|
| 91 |
with open(temp_path, "w", encoding="utf-8") as f:
|
| 92 |
f.writelines(lines)
|
| 93 |
|
| 94 |
api.upload_file( # reupload full file
|
| 95 |
path_or_fileobj=temp_path,
|
| 96 |
path_in_repo="feedback.jsonl",
|
| 97 |
-
repo_id=
|
| 98 |
-
repo_type="
|
| 99 |
token=HF_TOKEN,
|
| 100 |
commit_message="New feedback added"
|
| 101 |
)
|
|
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
modelName = "MitchellKil/gaelic-ipa-byt5" # my fine tuned model
|
| 15 |
+
feedbackFile = "mitchellkil/gaelic-app-feedback" # file to write feedback to
|
| 16 |
|
| 17 |
HF_TOKEN = os.environ["HF_TOKEN"] # set in secrets
|
| 18 |
|
|
|
|
| 27 |
|
| 28 |
print("Model loaded")
|
| 29 |
|
| 30 |
+
class gaelicText(BaseModel):
|
| 31 |
text: str
|
| 32 |
|
| 33 |
|
| 34 |
+
def gaelicToIPA(text: str) -> str:
|
| 35 |
# Input text - > target text for trained model
|
| 36 |
prompt = f"convert Gaelic to IPA: {text}"
|
| 37 |
|
|
|
|
| 52 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
return decoded.strip()
|
| 54 |
|
| 55 |
+
@app.post("/predict") # call to actually run and return text to project
|
| 56 |
+
def predict(request: gaelicText):
|
| 57 |
+
ipa = gaelicToIPA(request.text)
|
| 58 |
+
return {"ipa": ipa}
|
| 59 |
+
|
| 60 |
class Feedback(BaseModel):
|
| 61 |
type: str
|
| 62 |
feedback: str
|
|
|
|
| 64 |
translation: str
|
| 65 |
phonetic: str
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
@app.post("/feedback")
|
| 69 |
def save_feedback(data: Feedback):
|
| 70 |
+
feedbackText = {
|
| 71 |
"type": data.type,
|
| 72 |
"timestamp": datetime.utcnow().isoformat(),
|
| 73 |
"input_text": data.input_text,
|
|
|
|
| 77 |
}
|
| 78 |
try: # gets current feedback file
|
| 79 |
local_file = hf_hub_download(
|
| 80 |
+
repo_id=dataset,
|
| 81 |
filename="feedback.jsonl",
|
| 82 |
repo_type="dataset",
|
| 83 |
token=HF_TOKEN
|
| 84 |
)
|
| 85 |
with open(local_file, "r", encoding="utf-8") as f:
|
| 86 |
lines = f.readlines()
|
| 87 |
+
except Exception:
|
| 88 |
+
lines = []
|
| 89 |
|
| 90 |
+
lines.append(json.dumps(feedbackText) + "\n") # append
|
| 91 |
|
| 92 |
+
temp_path = "feedbackTemp.jsonl" # create temp file
|
| 93 |
with open(temp_path, "w", encoding="utf-8") as f:
|
| 94 |
f.writelines(lines)
|
| 95 |
|
| 96 |
api.upload_file( # reupload full file
|
| 97 |
path_or_fileobj=temp_path,
|
| 98 |
path_in_repo="feedback.jsonl",
|
| 99 |
+
repo_id=dataset,
|
| 100 |
+
repo_type="feedbackFile",
|
| 101 |
token=HF_TOKEN,
|
| 102 |
commit_message="New feedback added"
|
| 103 |
)
|