bekirmfr commited on
Commit
bf31b48
1 Parent(s): 509b86f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -22
app.py CHANGED
@@ -1,23 +1,166 @@
1
- from flask import Flask, request, jsonify
2
- from transformers import pipeline
3
-
4
- app = Flask(__name__)
5
-
6
- # Load the model
7
- generator = pipeline('text-generation', model='Rhea-72b-v0.5')
8
-
9
- @app.route('/generate', methods=['POST'])
10
- def generate_text():
11
- try:
12
- data = request.json
13
- prompt = data['prompt']
14
- max_length = data.get('max_length', 50) # Default max_length to 50 if not provided
15
-
16
- # Generate text
17
- generated = generator(prompt, max_length=max_length, return_full_text=False)
18
- return jsonify(generated[0]), 200
19
- except Exception as e:
20
- return jsonify({'error': str(e)}), 500
21
 
22
- if __name__ == '__main__':
23
- app.run(debug=True, host='0.0.0.0', port=5000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ 2
3
+ 3
4
+ 4
5
+ 5
6
+ 6
7
+ 7
8
+ 8
9
+ 9
10
+ 10
11
+ 11
12
+ 12
13
+ 13
14
+ 14
15
+ 15
16
+ 16
17
+ 17
18
+ 18
19
+ 19
20
+ 20
21
+ 21
22
+ 22
23
+ 23
24
+ 24
25
+ 25
26
+ 26
27
+ 27
28
+ 28
29
+ from fastapi import FastAPI
30
+ from transformers import pipeline
31
+
32
+ # Create a new FastAPI app instance
33
+ app = FastAPI()
34
+
35
+ # Initialize the text generation pipeline
36
+ # This function will be able to generate text
37
+ # given an input.
38
+ pipe = pipeline("text2text-generation",
39
+ model="
40
+ 2
41
+ 3
42
+ 4
43
+ 5
44
+ 6
45
+ 7
46
+ 8
47
+ 9
48
+ 10
49
+ 11
50
+ 12
51
+ 13
52
+ 14
53
+ 15
54
+ 16
55
+ 17
56
+ 18
57
+ 19
58
+ 20
59
+ 21
60
+ 22
61
+ 23
62
+ 24
63
+ 25
64
+ 26
65
+ 27
66
+ 28
67
+ from fastapi import FastAPI
68
+ from transformers import pipeline
69
+
70
+ # Create a new FastAPI app instance
71
+ app = FastAPI()
72
+
73
+ # Initialize the text generation pipeline
74
+ # This function will be able to generate text
75
+ # given an input.
76
+ pipe = pipeline("text2text-generation",
77
+ model="
78
+ 2
79
+ 3
80
+ 4
81
+ 5
82
+ 6
83
+ 7
84
+ 8
85
+ 9
86
+ 10
87
+ 11
88
+ 12
89
+ 13
90
+ 14
91
+ 15
92
+ 16
93
+ 17
94
+ 18
95
+ 19
96
+ 20
97
+ 21
98
+ 22
99
+ 23
100
+ 24
101
+ 25
102
+ 26
103
+ 27
104
+ 28
105
+ from fastapi import FastAPI
106
+ from transformers import pipeline
107
+
108
+ # Create a new FastAPI app instance
109
+ app = FastAPI()
110
+
111
+ # Initialize the text generation pipeline
112
+ # This function will be able to generate text
113
+ # given an input.
114
+ pipe = pipeline("text2text-generation",
115
+ model="Rhea-72b-v0.5")
116
+
117
+ # Define a function to handle the GET request at `/generate`
118
+ # The generate() function is defined as a FastAPI route that takes a
119
+ # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
120
+ # containing the generated text under the key "output"
121
+ @app.get("/generate")
122
+ def generate(text: str):
123
+ """
124
+ Using the text2text-generation pipeline from `transformers`, generate text
125
+ from the given input text. The model used is `google/flan-t5-small`, which
126
+ can be found [here](<https://huggingface.co/google/flan-t5-small>).
127
+ """
128
+ # Use the pipeline to generate text from the given input text
129
+ output = pipe(text)
130
+
131
+ # Return the generated text in a JSON response
132
+ return {"output": output[0]["generated_text"]}")
133
+
134
+ # Define a function to handle the GET request at `/generate`
135
+ # The generate() function is defined as a FastAPI route that takes a
136
+ # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
137
+ # containing the generated text under the key "output"
138
+ @app.get("/generate")
139
+ def generate(text: str):
140
+ """
141
+ Using the text2text-generation pipeline from `transformers`, generate text
142
+ from the given input text. The model used is `google/flan-t5-small`, which
143
+ can be found [here](<https://huggingface.co/google/flan-t5-small>).
144
+ """
145
+ # Use the pipeline to generate text from the given input text
146
+ output = pipe(text)
147
+
148
+ # Return the generated text in a JSON response
149
+ return {"output": output[0]["generated_text"]}")
150
+
151
+ # Define a function to handle the GET request at `/generate`
152
+ # The generate() function is defined as a FastAPI route that takes a
153
+ # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
154
+ # containing the generated text under the key "output"
155
+ @app.get("/generate")
156
+ def generate(text: str):
157
+ """
158
+ Using the text2text-generation pipeline from `transformers`, generate text
159
+ from the given input text. The model used is `google/flan-t5-small`, which
160
+ can be found [here](<https://huggingface.co/google/flan-t5-small>).
161
+ """
162
+ # Use the pipeline to generate text from the given input text
163
+ output = pipe(text)
164
+
165
+ # Return the generated text in a JSON response
166
+ return {"output": output[0]["generated_text"]}