File size: 2,027 Bytes
e045987 bf97a50 e045987 905c5a5 e045987 905c5a5 e045987 52906e9 e045987 905c5a5 e045987 905c5a5 e045987 bf97a50 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 905c5a5 e045987 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import gradio as gr
import requests
import os
token = os.getenv("HF_TOKEN")
# Function to generate text
def generate_text(prompt, model_choice, max_tokens, *other_params):
# Model name matching с URL
model_urls = {
"GPT-3.5": "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fmistralai%2FMixtral-8x7B-Instruct-v0.1%26quot%3B%3C%2Fspan%3E%2C
"GPT-4": "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fai-forever%2FruGPT-4%26quot%3B%3C%2Fspan%3E
}
# Selecting a URL depending on the selected model
api_url = model_urls[model_choice]
# Preparing data for a request
headers = {
"Authorization": f"Bearer {token}"
}
payload = {
"inputs": prompt,
"parameters": {"max_length": max_tokens},
"options": {"use_cache": False}
}
# Sending a request to API
response = requests.post(api_url, headers=headers, json=payload)
if response.status_code == 200:
# Returning the generated text
return response.json()[0]['generated_text']
else:
# Returning an error message
return "Error: " + response.text
# Creating an interface using G Blocks
with gr.Blocks() as demo:
with gr.Tab("Basic settings"):
with gr.Row():
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Enter text...")
model_choice = gr.Radio(["Mixtral-8x7B", "GPT-4"], label="Model selection", value="GPT-3.5")
with gr.Tab("Advanced settings"):
with gr.Row():
max_tokens = gr.Slider(100, 5000, step=1, label="Maximum tokens")
# Here you can add other parameters for the text generation API
with gr.Row():
generate_btn = gr.Button("Generation")
with gr.Row():
output_text = gr.Textbox(label="Answer", placeholder="The generated text will be here...")
# Setting up a callback function for the button
generate_btn.click(
fn=generate_text,
inputs=[prompt, model_choice, max_tokens],
outputs=output_text
)
# Launching the interface
demo.launch() |