subham1707 commited on
Commit
9588896
Β·
verified Β·
1 Parent(s): 3e6d745

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -65
app.py CHANGED
@@ -1,65 +1,58 @@
1
- #import gradio as gr
2
-
3
- #def greet(name):
4
- # return "Hello " + name + "!!"
5
-
6
- #demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- #demo.launch()
8
-
9
- #######################
10
-
11
- import spaces
12
-
13
- @spaces.GPU
14
- def main():
15
- # Your existing Gradio app code here
16
- import os
17
- import gradio as gr
18
- from transformers import AutoTokenizer, AutoModelForCausalLM
19
- import torch
20
-
21
- os.system("pip install gradio==3.50.2")
22
- model_id = "codellama/CodeLlama-7b-Instruct-hf"
23
- tokenizer = AutoTokenizer.from_pretrained(model_id)
24
- model = AutoModelForCausalLM.from_pretrained(
25
- model_id,
26
- device_map="auto",
27
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
28
- )
29
- tokenizer.pad_token = tokenizer.eos_token
30
-
31
- def convert_python_to_r(python_code):
32
- prompt = f"""### Task:
33
- Convert the following Python code to equivalent R code.
34
-
35
- ### Python code:
36
- {python_code}
37
-
38
- ### R code:"""
39
- input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids
40
- if torch.cuda.is_available():
41
- input_ids = input_ids.to("cuda")
42
-
43
- outputs = model.generate(
44
- input_ids,
45
- max_length=1024,
46
- do_sample=True,
47
- temperature=0.2,
48
- pad_token_id=tokenizer.eos_token_id,
49
- num_return_sequences=1
50
- )
51
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
52
- if "### R code:" in generated_text:
53
- generated_text = generated_text.split("### R code:")[-1].strip()
54
- return generated_text
55
-
56
- gr.Interface(
57
- fn=convert_python_to_r,
58
- inputs=gr.Textbox(lines=10, placeholder="Paste your Python code here..."),
59
- outputs="text",
60
- title="Python to R Code Converter using CodeLlama 7B Instruct",
61
- description="Enter Python code below, and the tool will convert it to R code using the CodeLlama 7B Instruct model."
62
- ).launch()
63
-
64
- if __name__ == "__main__":
65
- main()
 
1
+ import gradio as gr
2
+ from transformers import AutoModel, AutoProcessor
3
+ import torch
4
+ import requests
5
+ from PIL import Image
6
+ from io import BytesIO
7
+
8
+ fashion_items = ['top', 'trousers', 'jumper']
9
+
10
+ # Load model and processor
11
+ model_name = 'Marqo/marqo-fashionSigLIP'
12
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
13
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
14
+
15
+ # Preprocess and normalize text data
16
+ with torch.no_grad():
17
+ # Ensure truncation and padding are activated
18
+ processed_texts = processor(
19
+ text=fashion_items,
20
+ return_tensors="pt",
21
+ truncation=True, # Ensure text is truncated to fit model input size
22
+ padding=True # Pad shorter sequences so that all are the same length
23
+ )['input_ids']
24
+
25
+ text_features = model.get_text_features(processed_texts)
26
+ text_features = text_features / text_features.norm(dim=-1, keepdim=True)
27
+
28
+ # Prediction function
29
+ def predict_from_url(url):
30
+ # Check if the URL is empty
31
+ if not url:
32
+ return {"Error": "Please input a URL"}
33
+
34
+ try:
35
+ image = Image.open(BytesIO(requests.get(url).content))
36
+ except Exception as e:
37
+ return {"Error": f"Failed to load image: {str(e)}"}
38
+
39
+ processed_image = processor(images=image, return_tensors="pt")['pixel_values']
40
+
41
+ with torch.no_grad():
42
+ image_features = model.get_image_features(processed_image)
43
+ image_features = image_features / image_features.norm(dim=-1, keepdim=True)
44
+ text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
45
+
46
+ return {fashion_items[i]: float(text_probs[0, i]) for i in range(len(fashion_items))}
47
+
48
+ # Gradio interface
49
+ demo = gr.Interface(
50
+ fn=predict_from_url,
51
+ inputs=gr.Textbox(label="Enter Image URL"),
52
+ outputs=gr.Label(label="Classification Results"),
53
+ title="Fashion Item Classifier",
54
+ allow_flagging="never"
55
+ )
56
+
57
+ # Launch the interface
58
+ demo.launch()