Do0rMaMu commited on
Commit
843a03b
1 Parent(s): 3246b2f

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -0
main.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import ollama
3
+
4
+ app = Flask(__name__)
5
+
6
+
7
+ @app.route('/generate', methods=['POST'])
8
+ def generate_response():
9
+ try:
10
+ # Get input text from the request
11
+ input_text = request.json.get('input_text')
12
+ # Ensure the user message contains content
13
+ if input_text:
14
+ # Send user message through the chat stream
15
+ # chat_stream.send(messages = {'role': 'user', 'content': input_text})
16
+ chat_stream = ollama.chat(
17
+ model="mistral",
18
+ messages=[{'role': 'user', 'content': input_text}],
19
+ stream=True
20
+ )
21
+
22
+ print('text sent to model ')
23
+ # Collect response chunks from the stream
24
+ response_chunks = []
25
+ for chunk in chat_stream:
26
+ response_chunks.append(chunk['message']['content'])
27
+
28
+ # Check for Mistral's "end_of_response" signal
29
+ if chunk['message']['role'] == 'system' and chunk['message'].get('end_of_response'):
30
+ break
31
+
32
+ # Complete response
33
+ response = ''.join(response_chunks)
34
+ print('response ' , response)
35
+ return jsonify({"response": response})
36
+ else:
37
+ return jsonify({"error": "User message must contain content."})
38
+
39
+ except Exception as e:
40
+ return jsonify({"error": str(e)})
41
+
42
+ if __name__ == '__main__':
43
+ app.run(host='0.0.0.0', port=8000)