Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,20 @@
|
|
1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
-
import torch
|
3 |
import gradio as gr
|
4 |
|
5 |
-
# Load model
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
model.to(device)
|
13 |
|
14 |
-
def generate_code(prompt, max_length=100):
|
15 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
16 |
-
outputs = model.generate(
|
17 |
-
**inputs,
|
18 |
-
max_length=len(inputs["input_ids"][0]) + max_length,
|
19 |
-
temperature=0.7,
|
20 |
-
do_sample=True,
|
21 |
-
top_p=0.95,
|
22 |
-
pad_token_id=tokenizer.eos_token_id
|
23 |
-
)
|
24 |
-
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
-
return generated_code
|
26 |
-
|
27 |
-
# Gradio Interface
|
28 |
interface = gr.Interface(
|
29 |
fn=generate_code,
|
30 |
inputs=[
|
31 |
-
gr.Textbox(lines=5,
|
32 |
gr.Slider(20, 300, step=10, label="Max Tokens", value=100),
|
33 |
],
|
34 |
outputs=gr.Textbox(label="Generated Code"),
|
35 |
-
title="🧠 Code Generator
|
36 |
-
description="
|
37 |
)
|
38 |
|
39 |
if __name__ == "__main__":
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Load a pre-hosted code-generation model Space (no token needed)
|
4 |
+
generator = gr.Interface.load("huggingface/HuggingFaceH4/starchat-alpha")
|
|
|
|
|
5 |
|
6 |
+
def generate_code(prompt, max_tokens):
|
7 |
+
return generator(prompt, max_tokens)[0]
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
interface = gr.Interface(
|
10 |
fn=generate_code,
|
11 |
inputs=[
|
12 |
+
gr.Textbox(label="Code Prompt", lines=5, placeholder="e.g., def add(a, b):"),
|
13 |
gr.Slider(20, 300, step=10, label="Max Tokens", value=100),
|
14 |
],
|
15 |
outputs=gr.Textbox(label="Generated Code"),
|
16 |
+
title="🧠 Code Generator (No Token Required)",
|
17 |
+
description="Powered by Hugging Face hosted model. No API key needed."
|
18 |
)
|
19 |
|
20 |
if __name__ == "__main__":
|