made1570 commited on
Commit
f900ba5
·
verified ·
1 Parent(s): c0ba723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -33
app.py CHANGED
@@ -1,41 +1,26 @@
1
- import torch
2
- from transformers import AutoProcessor, AutoModelForImageTextToText
3
- from peft import PeftModel
4
  import gradio as gr
 
 
5
 
6
- # Set up device (CPU or GPU)
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
8
-
9
- # Load processor and model
10
- model_name = "adarsh3601/my_gemma3_pt" # Change to your model path
11
  processor = AutoProcessor.from_pretrained(model_name)
12
- model = AutoModelForImageTextToText.from_pretrained(model_name).to(device)
13
 
14
- # Optional: If using PEFT model with adapter
15
- # adapter_model_id = "your_adapter_model_id" # Uncomment and replace if using adapter
16
- # model = PeftModel.from_pretrained(model, adapter_model_id)
17
 
18
- # Define function to process the user input
19
- def chat(prompt):
20
- # Prepare the message in the format the model expects
21
- messages = [{"role": "user", "content": prompt}]
22
-
23
- # Process the input using the processor
24
  inputs = processor(messages, return_tensors="pt").to(device)
25
-
26
- # Generate the output from the model
27
- with torch.no_grad():
28
- outputs = model.generate(**inputs, max_length=200)
29
-
30
- # Decode and return the response
31
  return processor.decode(outputs[0], skip_special_tokens=True)
32
 
33
- # Gradio interface
34
- gr.Interface(
35
- fn=chat,
36
- inputs="text",
37
- outputs="text",
38
- title="Gemma Chat Model",
39
- description="Chat with Gemma3 model",
40
- live=True
41
- ).launch(share=False) # share=False for Hugging Face Spaces
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModel, AutoProcessor
3
+ import torch
4
 
5
+ # Load model and processor
6
+ model_name = "adarsh3601/my_gemma3_pt"
7
+ model = AutoModel.from_pretrained(model_name)
 
 
8
  processor = AutoProcessor.from_pretrained(model_name)
 
9
 
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model.to(device)
 
12
 
13
+ # Define a function to handle the chat interface
14
+ def chat(messages):
 
 
 
 
15
  inputs = processor(messages, return_tensors="pt").to(device)
16
+ outputs = model.generate(**inputs)
 
 
 
 
 
17
  return processor.decode(outputs[0], skip_special_tokens=True)
18
 
19
+ # Create the Gradio interface
20
+ iface = gr.Interface(fn=chat,
21
+ inputs=gr.Textbox(label="Your Message", lines=7),
22
+ outputs=gr.Textbox(label="Response"),
23
+ live=True)
24
+
25
+ # Launch the app
26
+ iface.launch(debug=True)