AdarshJi commited on
Commit
30e4b7a
1 Parent(s): 9727aac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ import uvicorn
5
+ import json
6
+ import requests
7
+ from flask import Flask, request, jsonify
8
+
9
+
10
+
11
+ app = Flask(__name__)
12
+
13
+ rq = requests.sessions.Session()
14
+
15
+ model_names = [
16
+ "meta-llama/Meta-Llama-3-70B-Instruct",
17
+ "meta-llama/Meta-Llama-3-8B-Instruct",
18
+ "mistralai/Mixtral-8x22B-Instruct-v0.1",
19
+ "mistralai/Mixtral-8x22B-v0.1",
20
+ "microsoft/WizardLM-2-8x22B",
21
+ "microsoft/WizardLM-2-7B",
22
+ "HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1",
23
+ "google/gemma-1.1-7b-it",
24
+ "databricks/dbrx-instruct",
25
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
26
+ "mistralai/Mistral-7B-Instruct-v0.2",
27
+ "meta-llama/Llama-2-70b-chat-hf",
28
+ "cognitivecomputations/dolphin-2.6-mixtral-8x7b",
29
+ "codellama/CodeLlama-70b-Instruct-hf"
30
+ ]
31
+
32
+
33
+
34
+
35
+ def DeepinFra(Api:str, messages:list ,model:str, max_tokens: int = 512, temperature: float = 0.7):
36
+
37
+ url = "https://api.deepinfra.com/v1/openai/chat/completions"
38
+ headers ={
39
+ "Authorization" : f"Bearer {Api}"
40
+ }
41
+
42
+
43
+
44
+
45
+ data = json.dumps(
46
+ {
47
+ 'model': model,
48
+ 'messages': messages,
49
+ 'temperature': temperature,
50
+ 'max_tokens': max_tokens,
51
+ 'stop': [],
52
+ 'stream': False
53
+ }, separators=(',', ':')
54
+ )
55
+
56
+ try:
57
+ result = rq.post(url=url, headers=headers, data=data)
58
+
59
+ return result.json()['choices'][0]['message']['content']
60
+ except:
61
+
62
+
63
+ return "Response content: " + result.text
64
+
65
+
66
+
67
+
68
+ @app.route("/generate-text-deep", methods=["POST"])
69
+ def generate_text():
70
+ data = request.json
71
+ message = data.get("message")
72
+ Api = data.get("api_key")
73
+ model_name = data.get("model_name", "mistralai/Mixtral-8x22B-Instruct-v0.1")
74
+ max_tokens = data.get("max_tokens", 512)
75
+ temperature = data.get("temperature", 0.7)
76
+
77
+ if not message or not Api:
78
+ return jsonify({"error": "Missing required fields"}), 400
79
+
80
+ response = DeepinFra(Api=Api , messages=message, model=model_name, max_tokens=max_tokens, temperature=temperature)
81
+
82
+ return jsonify({"response": response}), 200