File size: 1,091 Bytes
dcdccdb
aaae7a3
 
 
ab580b5
657577a
ac9937b
 
 
 
 
 
ab580b5
 
dcdccdb
68c2452
 
 
152d327
 
68c2452
 
aaae7a3
152d327
aaae7a3
152d327
aaae7a3
 
ab580b5
 
152d327
 
ac9937b
dcdccdb
 
820413a
dcdccdb
ac9937b
73f863d
ab580b5
 
aaae7a3
dcdccdb
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
36
37
38
39
40
41
42
43
import gradio as gr
import requests
import json
import os

API_URL = "https://api-inference.huggingface.co/models/openai-community/gpt2"
API_KEY = os.getenv('API_KEY')

if not API_KEY:
    raise ValueError("API_KEY environment variable not found. Please set it in your Secrets on Hugging Face.")

HEADERS = {"Authorization": f"Bearer {API_KEY}"}


def generate_text(prompt):
    payload = {
        "inputs": prompt,
        "parameters": {
            "max_length": 200,
            "do_sample": True
        }
    }
    response = requests.post(API_URL, headers=HEADERS, json=payload)
    
    if response.status_code == 200:
        return response.json()
    else:
        return f"Error: {response.status_code} - {response.text}"


description = "Enter your prompt and click Submit to see the model's response."
title = "Mistral Text Generation Interface"

api = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(label="Input Prompt", placeholder="Type something..."),
    outputs=gr.Textbox(label="Generated Text"),
    title=title,
    description=description
)


api.launch()