Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,41 @@
|
|
1 |
import torch
|
2 |
-
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
from peft import PeftModel
|
4 |
import gradio as gr
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
processor = AutoProcessor.from_pretrained(
|
12 |
-
model = AutoModelForImageTextToText.from_pretrained(
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
model.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
messages = []
|
28 |
-
|
29 |
-
# Format history into messages
|
30 |
-
for user_msg, bot_msg in history:
|
31 |
-
messages.append({"role": "user", "content": user_msg})
|
32 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
33 |
-
|
34 |
-
messages.append({"role": "user", "content": message})
|
35 |
-
prompt = format_chat(messages)
|
36 |
-
|
37 |
-
inputs = processor(prompt, return_tensors="pt").to(device)
|
38 |
-
|
39 |
with torch.no_grad():
|
40 |
-
outputs = model.generate(**inputs,
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
return response
|
45 |
|
46 |
# Gradio interface
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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_gemma_pt3" # 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
|