Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
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=
|
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-
|
55 |
-
description="Enter a prompt and generate text using
|
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)
|