Spaces:
Sleeping
Sleeping
Download app.py from Technobeast/Model-Server: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/Technobeast/Model-Server/resolve/main/app.py
- Command line
-
hf download hf://spaces/Technobeast/Model-Server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Technobeast/Model-Server/resolve/main/app.py
1.76 kB
| import uvicorn | |
| from flask import Flask, request, jsonify | |
| from huggingface_hub import InferenceClient | |
| app = Flask(__name__) | |
| API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1" | |
| def format_prompt(message, custom_instructions=None): | |
| prompt = "" | |
| if custom_instructions: | |
| prompt += f"[INST] {custom_instructions} [/INST]" | |
| prompt += f"[INST] {message} [/INST]" | |
| return prompt | |
| def Mistral7B(prompt, instructions, api, temperature=0.1, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0): | |
| global API_URL | |
| try: | |
| temperature = float(temperature) | |
| if temperature < 1e-2: | |
| temperature = 1e-2 | |
| top_p = float(top_p) | |
| generate_kwargs = dict( | |
| temperature=temperature, | |
| max_new_tokens=max_new_tokens, | |
| top_p=top_p, | |
| repetition_penalty=repetition_penalty, | |
| do_sample=True, | |
| seed=69, | |
| ) | |
| custom_instructions = instructions | |
| formatted_prompt = format_prompt(prompt, custom_instructions) | |
| head = {"Authorization": f"Bearer {api}"} | |
| client = InferenceClient(API_URL, headers=head) | |
| response = client.text_generation(formatted_prompt, **generate_kwargs) | |
| return response | |
| except Exception as e: | |
| return str(e) | |
| def generate_text(): | |
| data = request.json | |
| prompt = data.get("prompt") | |
| instructions = data.get("instructions") | |
| api_key = data.get("api_key") | |
| if not prompt or not instructions or not api_key: | |
| return jsonify({"error": "Missing required fields"}), 400 | |
| response = Mistral7B(prompt, instructions, api_key) | |
| return jsonify({"response": response}), 200 |