PyGooses commited on
Commit
cf32d26
·
verified ·
1 Parent(s): eb86ae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -1,11 +1,12 @@
1
- import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from hashlib import sha256
4
  import json
5
  import concurrent.futures
6
 
7
- hashed_api_key = "47d8666ed003a61b3bbc900ce94dc1ce8a1f9d0f24ee1ceb5e2f20381eb3f3a6"
8
 
 
9
  client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
10
 
11
  def generate_response(params):
@@ -17,17 +18,18 @@ def generate_response(params):
17
  except Exception as e:
18
  return f"An error occurred: {str(e)}"
19
 
20
- def respond(params_json, _):
 
 
21
  params = json.loads(params_json)
22
  api_key = params.pop("api_key", None)
23
  if sha256(api_key.encode('utf-8')).hexdigest() != hashed_api_key:
24
- return "Invalid API key"
25
 
26
  with concurrent.futures.ThreadPoolExecutor() as executor:
27
  future = executor.submit(generate_response, params)
28
- return future.result()
29
-
30
- demo = gr.ChatInterface(respond)
31
 
32
  if __name__ == "__main__":
33
- demo.launch()
 
1
+ from flask import Flask, request, jsonify
2
  from huggingface_hub import InferenceClient
3
  from hashlib import sha256
4
  import json
5
  import concurrent.futures
6
 
7
+ app = Flask(__name__)
8
 
9
+ hashed_api_key = "47d8666ed003a61b3bbc900ce94dc1ce8a1f9d0f24ee1ceb5e2f20381eb3f3a6"
10
  client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
11
 
12
  def generate_response(params):
 
18
  except Exception as e:
19
  return f"An error occurred: {str(e)}"
20
 
21
+ @app.route('/respond', methods=['POST'])
22
+ def respond():
23
+ params_json = request.json
24
  params = json.loads(params_json)
25
  api_key = params.pop("api_key", None)
26
  if sha256(api_key.encode('utf-8')).hexdigest() != hashed_api_key:
27
+ return jsonify({"error": "Invalid API key"}), 403
28
 
29
  with concurrent.futures.ThreadPoolExecutor() as executor:
30
  future = executor.submit(generate_response, params)
31
+ result = future.result()
32
+ return jsonify({"response": result})
 
33
 
34
  if __name__ == "__main__":
35
+ app.run(port=7860)