Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
#
|
5 |
-
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
|
6 |
-
|
7 |
-
# Load the tokenizer and model.
|
8 |
-
# Using device_map="auto" ensures that if a GPU is available (as on ZeroGPU), it is used.
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
""
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
# Wrap the result in a paragraph tag for HTML rendering.
|
27 |
-
recommendation = f"<p>{output[0]['generated_text']}</p>"
|
28 |
-
return recommendation
|
29 |
-
except Exception as e:
|
30 |
-
return f"<p style='color: red;'>Error: {str(e)}</p>"
|
31 |
|
32 |
-
#
|
33 |
with gr.Blocks() as demo:
|
34 |
-
gr.Markdown("
|
35 |
-
gr.
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
with gr.Column():
|
41 |
-
characteristics = gr.Textbox(
|
42 |
-
lines=10,
|
43 |
-
placeholder="Enter your characteristics here...",
|
44 |
-
label="Your Characteristics"
|
45 |
-
)
|
46 |
-
submit = gr.Button("Submit")
|
47 |
-
with gr.Column():
|
48 |
-
result = gr.HTML(label="Diet Recommendations")
|
49 |
-
|
50 |
-
# When the button is clicked, the app will show a progress spinner.
|
51 |
-
submit.click(
|
52 |
-
fn=generate_recommendation,
|
53 |
-
inputs=characteristics,
|
54 |
-
outputs=result,
|
55 |
-
show_progress=True
|
56 |
-
)
|
57 |
|
|
|
58 |
if __name__ == "__main__":
|
59 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
+
# Load tokenizer and model
|
6 |
+
model_name = "Qwen/Qwen2.5-0.5B-Instruct" # replace with actual model name or path
|
|
|
|
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
8 |
|
9 |
+
if torch.cuda.is_available():
|
10 |
+
# Load model in half precision and move to GPU for faster inference
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
12 |
+
model.to("cuda")
|
13 |
+
else:
|
14 |
+
# Load model on CPU (full precision by default)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
16 |
|
17 |
+
# Define the text generation function
|
18 |
+
def generate_text(prompt):
|
19 |
+
# Tokenize input and move to appropriate device
|
20 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
21 |
+
if torch.cuda.is_available():
|
22 |
+
inputs = {key: value.to("cuda") for key, value in inputs.items()}
|
23 |
+
# Generate text
|
24 |
+
output_ids = model.generate(**inputs, max_length=200, do_sample=True, temperature=0.7)
|
25 |
+
# Decode the generated tokens to text
|
26 |
+
generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
27 |
+
return generated_text
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Set up Gradio interface
|
30 |
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("## Text Generation Demo") # title or description (optional)
|
32 |
+
user_input = gr.Textbox(label="Enter your prompt:")
|
33 |
+
output_text = gr.Textbox(label="Generated output:")
|
34 |
+
generate_btn = gr.Button("Generate")
|
35 |
+
# When button is clicked, call generate_text with user_input and show in output_text
|
36 |
+
generate_btn.click(fn=generate_text, inputs=user_input, outputs=output_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Launch the app (if running as a standalone script)
|
39 |
if __name__ == "__main__":
|
40 |
demo.launch()
|