Technobeast commited on
Commit
a71e3c3
·
verified ·
1 Parent(s): 9ae2f03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+
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
+
10
+ def format_prompt(message, custom_instructions=None):
11
+ prompt = ""
12
+ if custom_instructions:
13
+ prompt += f"[INST] {custom_instructions} [/INST]"
14
+ prompt += f"[INST] {message} [/INST]"
15
+ return prompt
16
+
17
+ def Mistral7B(prompt, instructions, api, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0):
18
+ global API_URL
19
+ try:
20
+ temperature = float(temperature)
21
+ if temperature < 1e-2:
22
+ temperature = 1e-2
23
+ top_p = float(top_p)
24
+
25
+ generate_kwargs = dict(
26
+ temperature=temperature,
27
+ max_new_tokens=max_new_tokens,
28
+ top_p=top_p,
29
+ repetition_penalty=repetition_penalty,
30
+ do_sample=True,
31
+ seed=69,
32
+ )
33
+ custom_instructions = instructions
34
+ formatted_prompt = format_prompt(prompt, custom_instructions)
35
+
36
+ head = {"Authorization": f"Bearer {api}"}
37
+ client = InferenceClient(API_URL, headers=head)
38
+ response = client.text_generation(formatted_prompt, **generate_kwargs)
39
+ return response
40
+ except Exception as e:
41
+ return str(e)
42
+
43
+ @app.route("/generate-text", methods=["POST"])
44
+ def generate_text():
45
+ data = request.json
46
+ prompt = data.get("prompt")
47
+ instructions = data.get("instructions")
48
+ api_key = data.get("api_key")
49
+
50
+ if not prompt or not instructions or not api_key:
51
+ return jsonify({"error": "Missing required fields"}), 400
52
+
53
+ response = Mistral7B(prompt, instructions, api_key)
54
+
55
+ return jsonify({"response": response}), 200