Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load model
|
| 5 |
+
pipe = pipeline("text-generation", model="gpt2") # Replace with your Hugging Face model ID
|
| 6 |
+
|
| 7 |
+
def generate_response(prompt, max_length, temperature):
|
| 8 |
+
output = pipe(
|
| 9 |
+
prompt,
|
| 10 |
+
max_length=max_length,
|
| 11 |
+
temperature=temperature,
|
| 12 |
+
do_sample=True,
|
| 13 |
+
pad_token_id=pipe.tokenizer.eos_token_id
|
| 14 |
+
)
|
| 15 |
+
return output[0]["generated_text"]
|
| 16 |
+
|
| 17 |
+
# Gradio Interface
|
| 18 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 19 |
+
gr.Markdown("# 📝 AI Text Generator")
|
| 20 |
+
gr.Markdown("Enter your prompt and generate creative text using a Hugging Face model.")
|
| 21 |
+
|
| 22 |
+
with gr.Row():
|
| 23 |
+
with gr.Column(scale=2):
|
| 24 |
+
prompt = gr.Textbox(
|
| 25 |
+
label="Prompt",
|
| 26 |
+
placeholder="Type your prompt here...",
|
| 27 |
+
lines=4
|
| 28 |
+
)
|
| 29 |
+
max_length = gr.Slider(20, 500, value=200, step=10, label="Max Length")
|
| 30 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 31 |
+
generate_btn = gr.Button("Generate")
|
| 32 |
+
|
| 33 |
+
with gr.Column(scale=3):
|
| 34 |
+
output = gr.Textbox(label="Generated Output", lines=10)
|
| 35 |
+
|
| 36 |
+
generate_btn.click(
|
| 37 |
+
fn=generate_response,
|
| 38 |
+
inputs=[prompt, max_length, temperature],
|
| 39 |
+
outputs=[output]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
demo.launch()
|