hussein2000 commited on
Commit
0ede83c
·
verified ·
1 Parent(s): 25f2698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -35
app.py CHANGED
@@ -1,43 +1,40 @@
1
  from flask import Flask, request, jsonify
2
- import json
3
- import base64
4
- import requests
5
- app = Flask(__name__)
6
-
7
- # GitHub credentials
8
- GITHUB_TOKEN = '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' # Set your token in environment variables
9
- REPO_OWNER = 'hussein2000-oo'
10
- REPO_NAME = 'dbailloolloloolollhrthlnewrgnk'
11
- USER_FILE_NAME = 'user.json'
12
-
13
 
14
- # Function to fetch user data from GitHub
15
- def fetch_user_data():
16
- url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contents/{USER_FILE_NAME}'
17
- headers = {'Authorization': f'token {GITHUB_TOKEN}'}
18
- response = requests.get(url, headers=headers)
19
 
20
- if response.status_code == 200:
21
- content = response.json()
22
- user_data = json.loads(base64.b64decode(content['content']).decode('utf-8'))
23
- return user_data
24
- else:
25
- return None # Return None if fetching fails
26
-
27
-
28
- @app.route('/get_security_questions', methods=['POST'])
29
- def get_security_questions():
30
- data = request.json
31
- username = data.get('username')
32
 
33
- user_data = fetch_user_data()
 
 
 
34
 
35
- if user_data and username in user_data:
36
- questions = user_data[username]['security_questions']
37
- return jsonify({"security_questions": list(questions.keys())}), 200
38
- else:
39
- return jsonify({"error": "User not found or data fetch failed."}), 404
40
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == '__main__':
43
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, jsonify
2
+ from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # Initialize the InferenceClient with your API key
5
+ x="hf_uYlR"
6
+ y="EMsDNbxQPJCSAHgwthrylHZZKKmGyg"
7
+ u=x+y
8
+ client = InferenceClient(api_key=f"{u}")
9
 
10
+ # Create a Flask app
11
+ app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
12
 
13
+ @app.route('/chat', methods=['POST'])
14
+ def chat():
15
+ # Get user message from the request
16
+ user_message = request.json.get('message')
17
 
18
+ # Check if the user message is provided
19
+ if not user_message:
20
+ return jsonify({"error": "No message provided"}), 400
21
+
22
+ # Create a single message list for the request
23
+ messages = [{"role": "user", "content": user_message}]
24
+
25
+ # Create the chat completion request with the current message
26
+ response = client.chat.completions.create(
27
+ model="Qwen/Qwen2.5-72B-Instruct",
28
+ messages=messages,
29
+ max_tokens=1024,
30
+ stream=False # Set stream to False to get the full response at once
31
+ )
32
+
33
+ # Get the assistant's response
34
+ assistant_message = response.choices[0].message.content
35
+
36
+ # Return the assistant's response as JSON
37
+ return jsonify({"response": assistant_message})
38
 
39
  if __name__ == '__main__':
40
  app.run(host="0.0.0.0", port=7860)