Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
-
import spaces
|
3 |
import torch
|
4 |
-
from
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
"text-generation",
|
38 |
-
model=model,
|
39 |
-
tokenizer=tokenizer,
|
40 |
-
max_new_tokens=1500,
|
41 |
-
pad_token_id=tokenizer.eos_token_id
|
42 |
-
)
|
43 |
-
|
44 |
-
# Set up LangChain
|
45 |
-
llm = HuggingFacePipeline(pipeline=qa_pipeline)
|
46 |
-
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
47 |
-
qa_chain = RetrievalQA.from_chain_type(
|
48 |
-
retriever=retriever,
|
49 |
-
chain_type="stuff",
|
50 |
-
llm=llm,
|
51 |
-
return_source_documents=False
|
52 |
-
)
|
53 |
-
|
54 |
-
@spaces.GPU
|
55 |
-
def preprocess_query(query):
|
56 |
-
if "script" in query or "code" in query.lower():
|
57 |
-
return f"Write a CPSL script: {query}"
|
58 |
-
return query
|
59 |
-
|
60 |
-
@spaces.GPU
|
61 |
-
def clean_response(response):
|
62 |
-
result = response.get("result", "")
|
63 |
-
if "Answer:" in result:
|
64 |
-
return result.split("Answer:")[1].strip()
|
65 |
-
return result.strip()
|
66 |
-
|
67 |
-
@spaces.GPU
|
68 |
-
def chatbot_response(user_input):
|
69 |
-
processed_query = preprocess_query(user_input)
|
70 |
-
raw_response = qa_chain.invoke({"query": processed_query})
|
71 |
-
return clean_response(raw_response)
|
72 |
-
|
73 |
-
with gr.Blocks() as demo: # Removed @spaces.GPU here
|
74 |
-
gr.Markdown("# CPSL Chatbot")
|
75 |
-
chat_history = gr.Chatbot()
|
76 |
-
user_input = gr.Textbox(label="Your Message:")
|
77 |
-
send_button = gr.Button("Send")
|
78 |
-
|
79 |
-
@spaces.GPU
|
80 |
-
def interact(user_message, history):
|
81 |
-
bot_reply = chatbot_response(user_message)
|
82 |
-
history.append((user_message, bot_reply))
|
83 |
-
return history, history
|
84 |
-
|
85 |
-
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
86 |
-
|
87 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
model_id = "google/paligemma2-28b-mix-448"
|
8 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto").eval()
|
9 |
+
processor = PaliGemmaProcessor.from_pretrained(model_id)
|
10 |
+
|
11 |
+
def generate_description(image, prompt):
|
12 |
+
if image is None:
|
13 |
+
return "Please upload an image."
|
14 |
+
|
15 |
+
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(torch.bfloat16).to(model.device)
|
16 |
+
input_len = model_inputs["input_ids"].shape[-1]
|
17 |
+
|
18 |
+
with torch.inference_mode():
|
19 |
+
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
|
20 |
+
generation = generation[0][input_len:]
|
21 |
+
decoded = processor.decode(generation, skip_special_tokens=True)
|
22 |
+
|
23 |
+
return decoded
|
24 |
+
|
25 |
+
# Gradio UI
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("# PaliGemma Image Captioning")
|
28 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
29 |
+
prompt_input = gr.Textbox(label="Enter Prompt", value="describe en")
|
30 |
+
output_text = gr.Textbox(label="Generated Description")
|
31 |
+
submit_button = gr.Button("Generate")
|
32 |
+
|
33 |
+
submit_button.click(generate_description, inputs=[image_input, prompt_input], outputs=output_text)
|
34 |
+
|
35 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|