Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1" | |
| headers = { | |
| "Authorization": f"Bearer HF_API_TOKEN" | |
| } | |
| def query(payload): | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| if response.status_code != 200: | |
| return {"error": f"API error {response.status_code}: {response.text}"} | |
| return response.json() | |
| def process(prompt): | |
| output = query({ | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 100, | |
| "temperature": 0.7 | |
| } | |
| }) | |
| if "error" in output: | |
| return output["error"] | |
| if isinstance(output, list) and "generated_text" in output[0]: | |
| return output[0]["generated_text"].replace(prompt, "").strip() | |
| return f"Unexpected output: {output}" | |
| gr.Interface( | |
| fn=process, | |
| inputs=gr.Textbox(label="Prompt", lines=3), | |
| outputs=gr.Textbox(label="Response", lines=6), | |
| title="Hugging Face Mistral API Test" | |
| ).launch() | |