Spaces:
Sleeping
Sleeping
Erik commited on
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -1,19 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
def generate_text(prompt):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 13 |
with gr.Row():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the Hugging Face Inference client
|
| 5 |
+
client = InferenceClient()
|
| 6 |
|
| 7 |
def generate_text(prompt):
|
| 8 |
+
try:
|
| 9 |
+
# Generate text using the Inference API
|
| 10 |
+
generated_text = client.text_generation(
|
| 11 |
+
prompt,
|
| 12 |
+
model="HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 13 |
+
max_new_tokens=200,
|
| 14 |
+
do_sample=True,
|
| 15 |
+
temperature=0.7
|
| 16 |
+
)
|
| 17 |
+
return generated_text
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Error generating text: {str(e)}"
|
| 20 |
|
| 21 |
+
# Create the Gradio interface with modern theme
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown("# SmolLM Text Generation 🚀")
|
| 24 |
+
gr.Markdown("Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
|
| 25 |
+
|
| 26 |
with gr.Row():
|
| 27 |
+
with gr.Column():
|
| 28 |
+
input_text = gr.Textbox(
|
| 29 |
+
label="Input Prompt",
|
| 30 |
+
placeholder="Enter your prompt here...",
|
| 31 |
+
lines=5
|
| 32 |
+
)
|
| 33 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 34 |
+
|
| 35 |
+
with gr.Column():
|
| 36 |
+
output_text = gr.Textbox(
|
| 37 |
+
label="Generated Text",
|
| 38 |
+
interactive=False,
|
| 39 |
+
lines=10
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Add examples
|
| 43 |
+
examples = [
|
| 44 |
+
["Explain quantum computing in simple terms"],
|
| 45 |
+
["Write a short story about a robot learning to love"],
|
| 46 |
+
["How do I make a perfect omelette?"]
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
gr.Examples(
|
| 50 |
+
examples=examples,
|
| 51 |
+
inputs=input_text,
|
| 52 |
+
outputs=output_text,
|
| 53 |
+
fn=generate_text,
|
| 54 |
+
cache_examples=True
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
generate_btn.click(
|
| 58 |
+
fn=generate_text,
|
| 59 |
+
inputs=input_text,
|
| 60 |
+
outputs=output_text,
|
| 61 |
+
api_visibility="public"
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
# Launch with modern theme and settings
|
| 65 |
+
demo.launch(
|
| 66 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 67 |
+
footer_links=[
|
| 68 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 69 |
+
{"label": "Model", "url": "https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct"}
|
| 70 |
+
]
|
| 71 |
+
)
|