File size: 1,014 Bytes
045015b
5a0561f
045015b
5a0561f
 
fbc17c1
5a0561f
09bb332
5a0561f
 
 
 
 
49c746a
5a0561f
 
 
 
 
 
 
 
 
 
 
 
 
49c746a
5a0561f
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()