TheVera commited on
Commit
761f569
·
verified ·
1 Parent(s): e6e4784

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -1,16 +1,15 @@
1
- import uvicorn
2
  import os
 
3
  from flask import Flask, request, jsonify
4
  from huggingface_hub import InferenceClient
5
 
6
  app = Flask(__name__)
7
 
8
- # API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
9
- # Use environment variables for configuration
10
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
11
- # API_KEY = os.getenv("API_KEY", "your_default_api_key") # Default API_KEY can be set here
12
- API_KEY = os.getenv("API_KEY")
13
 
 
 
14
 
15
  def format_prompt(message, custom_instructions=None):
16
  prompt = ""
@@ -34,9 +33,8 @@ def normalize_text(text):
34
  text = text.replace(british, american)
35
 
36
  return text
37
-
38
  def Mistral7B(prompt, instructions, api_key, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0):
39
- global API_URL
40
  try:
41
  temperature = max(float(temperature), 1e-2)
42
  top_p = float(top_p)
@@ -52,13 +50,12 @@ def Mistral7B(prompt, instructions, api_key, temperature=0.1, max_new_tokens=2,
52
  custom_instructions = instructions
53
  formatted_prompt = format_prompt(prompt, custom_instructions)
54
 
55
- client = InferenceClient(api_url=API_URL, token=api_key)
56
- response = client.text_generation(formatted_prompt, **generate_kwargs)
57
  return response
58
  except Exception as e:
59
  return str(e)
60
 
61
-
62
  @app.route("/generate-text", methods=["POST"])
63
  def generate_text():
64
  data = request.json
@@ -75,4 +72,4 @@ def generate_text():
75
  return jsonify({"response": response}), 200
76
 
77
  if __name__ == "__main__":
78
- uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))
 
 
1
  import os
2
+ import uvicorn
3
  from flask import Flask, request, jsonify
4
  from huggingface_hub import InferenceClient
5
 
6
  app = Flask(__name__)
7
 
8
+ # Fixed API URL
9
+ MODEL_ID = "mistralai/Mixtral-8x7B-Instruct-v0.1"
 
 
 
10
 
11
+ # Use environment variable for the API key
12
+ API_KEY = os.getenv("API_KEY")
13
 
14
  def format_prompt(message, custom_instructions=None):
15
  prompt = ""
 
33
  text = text.replace(british, american)
34
 
35
  return text
36
+
37
  def Mistral7B(prompt, instructions, api_key, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0):
 
38
  try:
39
  temperature = max(float(temperature), 1e-2)
40
  top_p = float(top_p)
 
50
  custom_instructions = instructions
51
  formatted_prompt = format_prompt(prompt, custom_instructions)
52
 
53
+ client = InferenceClient(token=api_key)
54
+ response = client.text_generation(formatted_prompt, model=MODEL_ID, **generate_kwargs)
55
  return response
56
  except Exception as e:
57
  return str(e)
58
 
 
59
  @app.route("/generate-text", methods=["POST"])
60
  def generate_text():
61
  data = request.json
 
72
  return jsonify({"response": response}), 200
73
 
74
  if __name__ == "__main__":
75
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 8000)))