Spaces:
Sleeping
Sleeping
File size: 1,561 Bytes
6c4a93d 1721515 b7f1129 1ce6a12 f5cb146 b7f1129 4779f2c b7f1129 12c1e66 cae0258 1ce6a12 4779f2c 6c4a93d cae0258 07b3a85 1721515 cae0258 07b3a85 4779f2c b7f1129 |
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 |
import gradio as gr
import openai
from huggingface_hub import HfApi
import os
# Initialize Hugging Face API to fetch secrets
api = HfApi()
# Fetch the OpenAI API key from Hugging Face secrets
openai.api_key = api.get_repo_secret(
repo_id="tchans123/gpt2-deployment", # Replace with your Hugging Face repository ID
secret_name="OPENAI_API_KEY" # Name of the secret in Hugging Face
)
def generate_text(prompt, max_length=100, temperature=1.0):
try:
# Generate text using OpenAI's GPT-3 (text-davinci-003)
response = openai.Completion.create(
engine="text-davinci-003", # Specify the engine
prompt=prompt,
max_tokens=max_length,
temperature=temperature,
n=1,
stop=None
)
return response.choices[0].text.strip()
except openai.error.OpenAIError as e:
return f"An OpenAI error occurred: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
# Create a Gradio interface
iface = gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(lines=3, placeholder="Enter your prompt here...", label="Prompt"),
gr.Slider(minimum=50, maximum=500, value=100, label="Max Length"),
gr.Slider(minimum=0.1, maximum=1.5, value=1.0, label="Temperature"),
],
outputs=gr.Textbox(label="Generated Text"),
title="GPT Text Generator",
description="Enter a prompt and generate text using OpenAI's GPT (text-davinci-003)."
)
# Launch the interface
iface.launch(share=True)
|