tchans123 commited on
Commit
5340d4e
1 Parent(s): 85cc6c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -45
app.py CHANGED
@@ -1,59 +1,25 @@
1
  import gradio as gr
2
- import openai
3
- import os
4
 
5
- # Retrieve the OpenAI API key from Hugging Face Secrets
6
- openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- # Check if the API key is correctly set
9
- if openai.api_key is None:
10
- raise ValueError("OpenAI API key is not set. Please configure the 'OPENAI_API_KEY' secret in your Hugging Face Space settings.")
11
-
12
- def generate_text(prompt, max_length=100, temperature=1.0):
13
- try:
14
- # Debug: Print the input prompt and settings
15
- print(f"Prompt: {prompt}")
16
- print(f"Max Length: {max_length}, Temperature: {temperature}")
17
-
18
- # Generate text using OpenAI's GPT-3 (text-davinci-003)
19
- response = openai.Completion.create(
20
- engine="text-davinci-003",
21
- prompt=prompt,
22
- max_tokens=max_length,
23
- temperature=temperature,
24
- n=1,
25
- stop=None
26
- )
27
-
28
- # Debug: Print the raw response from OpenAI
29
- print(f"API Response: {response}")
30
-
31
- # Extract and return the generated text
32
- generated_text = response.choices[0].text.strip()
33
- return generated_text
34
-
35
- except openai.error.OpenAIError as e:
36
- error_message = f"An OpenAI error occurred: {str(e)}"
37
- print(error_message)
38
- return error_message
39
-
40
- except Exception as e:
41
- error_message = f"An unexpected error occurred: {str(e)}"
42
- print(error_message)
43
- return error_message
44
 
45
  # Create a Gradio interface
46
  iface = gr.Interface(
47
  fn=generate_text,
48
  inputs=[
49
  gr.Textbox(lines=3, placeholder="Enter your prompt here...", label="Prompt"),
50
- gr.Slider(minimum=50, maximum=500, value=100, label="Max Length"),
51
- gr.Slider(minimum=0.1, maximum=1.5, value=1.0, label="Temperature"),
52
  ],
53
  outputs=gr.Textbox(label="Generated Text"),
54
- title="GPT-3 Text Generator",
55
- description="Enter a prompt and generate text using OpenAI's GPT-3 (text-davinci-003)."
56
  )
57
 
58
- # Launch the interface
59
  iface.launch(share=True)
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Load the GPT-2 model from Hugging Face using the pipeline
5
+ generator = pipeline('text-generation', model='gpt2')
6
 
7
+ def generate_text(prompt, max_length=50):
8
+ # Generate text using the GPT-2 model
9
+ generated_text = generator(prompt, max_length=max_length, num_return_sequences=1)[0]['generated_text']
10
+ return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Create a Gradio interface
13
  iface = gr.Interface(
14
  fn=generate_text,
15
  inputs=[
16
  gr.Textbox(lines=3, placeholder="Enter your prompt here...", label="Prompt"),
17
+ gr.Slider(minimum=10, maximum=100, value=50, label="Max Length"),
 
18
  ],
19
  outputs=gr.Textbox(label="Generated Text"),
20
+ title="GPT-2 Text Generator",
21
+ description="Enter a prompt and generate text using GPT-2 from Hugging Face."
22
  )
23
 
24
+ # Launch the Gradio interface
25
  iface.launch(share=True)