forked from ssbagpcm/sciencesproject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app.py
More file actions
66 lines (55 loc) · 1.87 KB
/
Copy pathflask_app.py
File metadata and controls
66 lines (55 loc) · 1.87 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from flask import Flask, request, jsonify, send_from_directory
from groq import Groq
import os
import markdown
app = Flask(__name__, static_url_path='', static_folder='.')
# api Groq
client = Groq(
api_key="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"
)
@app.route('/')
def index():
return app.send_static_file('sguibeta.html')
@app.route('/chatgpt')
def chatgpt():
return app.send_static_file('chatgpt.html')
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.json.get('message', '')
if not user_message:
return jsonify({'error': 'No message provided'}), 400
try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": user_message
}
],
model="llama-3.1-70b-versatile"
)
# Récupérer la réponse du modèle
response_message = chat_completion.choices[0].message.content
return jsonify({'response': response_message})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/files')
def get_files():
articles_dir = 'articles' # Assurez-vous que ce chemin est correct
files = [f for f in os.listdir(articles_dir) if f.endswith('.md')]
return jsonify(files)
@app.route('/api/file/<filename>')
def get_file_content(filename):
file_path = os.path.join('articles', filename)
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
html_content = markdown.markdown(content)
return html_content
return "File not found", 404
if __name__ == '__main__':
app.run(debug=True)