Kid Omar Costelo commited on
Commit
28c2d6f
·
1 Parent(s): 6eebd1b

Refactor app.py to remove unused code and update prompt_dir

Browse files
Files changed (1) hide show
  1. app.py +0 -65
app.py DELETED
@@ -1,65 +0,0 @@
1
- from transformers import pipeline
2
- from fastapi import FastAPI
3
- from pydantic import BaseModel
4
-
5
- # Getting the prompt from the prompt.txt file
6
- prompt_dir = "/prompt.txt"
7
- prompt = ''
8
- with open(prompt_dir, 'r') as file:
9
- prompt = file.read()
10
-
11
-
12
- def post_process(essay):
13
- # Find the index of the first occurrence of the word "Feedback:"
14
- feedback_index = essay.find("Feedback:")
15
-
16
- # If "Feedback:" is not found, return the original essay
17
- if feedback_index == -1:
18
- return essay
19
-
20
- # Find the index of the newline after the first occurrence of "Feedback:"
21
- newline_index = essay.find("\n", feedback_index)
22
-
23
- # If newline is not found, return the original essay
24
- if newline_index == -1:
25
- return essay
26
-
27
- # Return the essay up to the newline after the first occurrence of "Feedback:"
28
- return essay[:newline_index]
29
-
30
- def pre_process(instruction, essay):
31
- text = f"{instruction}\n{essay}"
32
- return text
33
-
34
-
35
- pipe = pipeline(
36
- "text-generation",
37
- model = "gildead/mistral-aes-414",
38
- device_map="auto"
39
- )
40
-
41
-
42
- class Message(BaseModel):
43
- essay: str
44
- instruction: str
45
-
46
- app = FastAPI()
47
-
48
-
49
- @app.get("/")
50
- async def root():
51
- return {"message": "Mistral API is running."}
52
-
53
- @app.post("/score")
54
- async def overall(message: Message):
55
- text = pre_process(message.instruction, message.essay)
56
- result = pipe(
57
- f"<s>[INST] {text} [/INST]",
58
- max_new_tokens=200,
59
- num_return_sequences=1,)
60
-
61
- generated_text = result[0]['generated_text']
62
- output = generated_text.split('[/INST]', 1)[-1].strip()
63
- final_output = post_process(output)
64
-
65
- return {"result": final_output}