Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
#
|
7 |
-
|
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 |
-
|
15 |
-
|
16 |
-
# model = PeftModel.from_pretrained(model, adapter_model_id)
|
17 |
|
18 |
-
# Define function to
|
19 |
-
def chat(
|
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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
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)
|
|