-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilename.py
35 lines (26 loc) · 1.76 KB
/
filename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask, request, jsonify
from transformers import AutoTokenizer, pipeline
app = Flask(__name__)
@app.route('/generate_text', methods=['POST'])
def generate_text():
data = request.get_json()
prompt = data.get('prompt', '')
instruction = f"### Instruction:\n{prompt}\n\n### Response:\n"
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-hf")
pipe = pipeline(task="text-generation", model="mayank200456789/results", tokenizer=tokenizer, max_length=128)
result = pipe(instruction)
generated_text = result[0]['generated_text']
instruction_start = generated_text.find('### Instruction:')
if instruction_start != -1:
instruction_end = generated_text[instruction_start + len('### Instruction:'):].find('###')
if instruction_end != -1:
first_instruction = generated_text[instruction_start + len('### Instruction:'):instruction_start + len('### Instruction:') + instruction_end].strip()
response_start = generated_text[instruction_start + instruction_end:].find('### Response:')
if response_start != -1:
response_end = generated_text[instruction_start + instruction_end + response_start + len('### Response:'):].find('###')
if response_end != -1:
first_response = generated_text[instruction_start + instruction_end + response_start + len('### Response:'):instruction_start + instruction_end + response_start + len('### Response:') + response_end].strip()
return jsonify({"first_instruction": first_instruction, "first_response": first_response})
return jsonify({"error": "Error occurred while processing the request."}), 500
if __name__ == '_main_':
app.run(debug=True)