tchans123 commited on
Commit
8f0e928
1 Parent(s): 335fea7
Files changed (2) hide show
  1. .env +1 -0
  2. app.py +16 -13
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY = org-TgJiwZlNrnXba0FGCx33ZaAs
app.py CHANGED
@@ -1,20 +1,23 @@
1
  import gradio as gr
2
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
 
3
 
4
- # Load the pre-trained GPT-2 model and tokenizer
5
- model_name = "gpt2" # You can change this to any other model like "gpt2-medium", "gpt2-large", etc.
6
- model = GPT2LMHeadModel.from_pretrained(model_name)
7
- tokenizer = GPT2Tokenizer.from_pretrained(model_name)
8
 
9
- # Function to generate text
10
- def generate_text(input_text):
11
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
12
- output = model.generate(input_ids, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, early_stopping=True)
13
- generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
14
- return generated_text
 
 
 
 
15
 
16
  # Create a Gradio interface
17
- iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="GPT-2 Text Generator", description="Enter a prompt to generate text using GPT-2.")
18
 
19
  # Launch the interface
20
- iface.launch(share=True)
 
1
  import gradio as gr
2
+ import openai
3
+ import os
4
 
5
+ # Set your OpenAI API key
6
+ openai.api_key = os.getenv("OPENAI_API_KEY") # Make sure to set this environment variable in Hugging Face Spaces
 
 
7
 
8
+ def generate_text(prompt):
9
+ response = openai.Completion.create(
10
+ engine="text-davinci-003", # You can use other engines like "text-curie-001" or "text-babbage-001"
11
+ prompt=prompt,
12
+ max_tokens=150,
13
+ n=1,
14
+ stop=None,
15
+ temperature=0.7,
16
+ )
17
+ return response.choices[0].text.strip()
18
 
19
  # Create a Gradio interface
20
+ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="GPT-3 Text Generator", description="Enter a prompt to generate text using GPT-3.")
21
 
22
  # Launch the interface
23
+ iface.launch(public=True)