Erik commited on
Commit
d5986d2
·
verified ·
1 Parent(s): c2d39a2

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +63 -11
app.py CHANGED
@@ -1,19 +1,71 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct")
5
- model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct")
6
 
7
  def generate_text(prompt):
8
- inputs = tokenizer(prompt, return_tensors="pt")
9
- outputs = model.generate(**inputs, max_length=200)
10
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
11
 
 
12
  with gr.Blocks() as demo:
 
 
 
13
  with gr.Row():
14
- input_text = gr.Textbox(label="Input Prompt")
15
- output_text = gr.Textbox(label="Generated Text")
16
- generate_btn = gr.Button("Generate")
17
- generate_btn.click(generate_text, inputs=input_text, outputs=output_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- demo.launch()
 
 
 
 
 
 
 
 
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
+ )