haielab commited on
Commit
fa49e9c
·
verified ·
1 Parent(s): fc4e311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -49
app.py CHANGED
@@ -1,59 +1,40 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
 
3
 
4
- # Specify the model name from HuggingFace Hub
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
- # Create a text-generation pipeline.
13
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
 
 
 
 
14
 
15
- def generate_recommendation(characteristics: str) -> str:
16
- """
17
- Generates personalized diet recommendations from the provided characteristics.
18
- """
19
- prompt = (
20
- f"Based on the following characteristics, provide personalized diet recommendations:\n\n"
21
- f"{characteristics}\n\n"
22
- f"Diet Recommendations:"
23
- )
24
- try:
25
- output = generator(prompt, max_length=512, num_return_sequences=1)
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
- # Build the Gradio Blocks interface.
33
  with gr.Blocks() as demo:
34
- gr.Markdown("# AI Diet Recommendations")
35
- gr.Markdown(
36
- "Get personalized diet recommendations generated by our AI."
37
- )
38
-
39
- with gr.Row():
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()