SarowarSaurav commited on
Commit
30f242a
1 Parent(s): 75e60e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -75
app.py CHANGED
@@ -1,86 +1,54 @@
1
- import pandas as pd
2
  import gradio as gr
3
- import hashlib
4
- from datetime import datetime
 
 
5
 
6
- # Function to create a vCard entry with UID and REV fields
7
- def create_vcf(row):
8
- # Generate a unique UID based on the email (using a hash for consistency)
9
- email = row['Email']
10
- uid = hashlib.md5(email.encode()).hexdigest()
11
- rev = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Current timestamp for revision
12
 
13
- vcf_template = """BEGIN:VCARD
14
- VERSION:3.0
15
- PRODID:-//Apple Inc.//iPhone OS 17.2.1//EN
16
- N:{name}
17
- FN:{name}
18
- ORG:BAT Bangladesh;{function}
19
- TITLE:{designation}
20
- TEL;TYPE=CELL;TYPE=VOICE;TYPE=pref:{number}
21
- EMAIL:{email}
22
- UID:{uid}
23
- REV:{rev}
24
- END:VCARD"""
25
- return vcf_template.format(
26
- name=row['Name'],
27
- function=row['Function'],
28
- designation=row['Designation'],
29
- number=row['Number'],
30
- email=email,
31
- uid=uid,
32
- rev=rev
33
- )
34
 
35
- # Function to generate the VCF file
36
- def generate_vcf(file):
37
- # Load the Excel file
38
- df = pd.read_excel(file)
39
- df['Number'] = df['Number'].astype(str) # Ensure phone numbers are strings
40
 
41
- # Generate the VCF data
42
- vcf_data = df.apply(create_vcf, axis=1).str.cat(sep='\n\n')
 
 
 
43
 
44
- # Write the VCF data to a file
45
- vcf_file_name = '/tmp/BAT_New_Contacts.vcf'
46
- with open(vcf_file_name, 'w') as vcf_file:
47
- vcf_file.write(vcf_data)
48
 
49
- return vcf_file_name
50
-
51
- # Custom CSS and HTML
52
- css = """
53
- .gradio-container {
54
- background: rgb(14, 43, 99);
55
- display: flex;
56
- flex-direction: column;
57
- align-items: center;
58
- color: #fff;
59
- padding: 20px;
60
- }
61
- .gradio-input, .gradio-output {
62
- background: rgb(28, 56, 113);
63
- color: #fff;
64
- }
65
- footer {
66
- display: none !important;
67
- }
68
- """
69
 
70
- html_content = """
71
- <div style="text-align: center; margin-bottom: 20px;">
72
- <img src="https://i.ibb.co/RbQRzcy/APMEA-CENTRAL-White.png" border="0" alt='BAT Bangladesh Logo' style='max-width: 300px;'>
73
- </div>
74
- <p style="text-align: center; color: #fff;">Upload an Excel file containing contact information in the following format: Name, Designation, Function, GRADE, Email, Number. The output will be a VCF file containing the contact information.</p>
75
  """
76
 
77
- # Gradio Interface
78
- with gr.Blocks(css=css) as demo:
79
- gr.HTML(html_content)
80
- gr.Interface(
81
- fn=generate_vcf,
82
- inputs=gr.File(label="Upload Excel File"),
83
- outputs=gr.File(label="Download VCF File"),
84
- )
 
85
 
86
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import ViTFeatureExtractor, ViTForImageClassification
4
+ from PIL import Image
5
+ import requests
6
 
7
+ # Load a pre-trained Vision Transformer model from Hugging Face
8
+ model_name = "nateraw/vit-base-patch16-224-in21k" # Replace with the model you've trained or a similar model
9
+ model = ViTForImageClassification.from_pretrained(model_name)
10
+ feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
 
 
11
 
12
+ # Define the disease labels (placeholders)
13
+ labels = {
14
+ 0: "Healthy",
15
+ 1: "Tobacco Mosaic Virus",
16
+ 2: "Brown Spot",
17
+ 3: "Frog Eye Leaf Spot",
18
+ 4: "Other"
19
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Define a function for disease detection
22
+ def detect_disease(image):
23
+ # Preprocess the image
24
+ inputs = feature_extractor(images=image, return_tensors="pt")
 
25
 
26
+ # Run the model
27
+ with torch.no_grad():
28
+ outputs = model(**inputs)
29
+ logits = outputs.logits
30
+ predicted_class = logits.argmax().item()
31
 
32
+ # Get disease name from label dictionary
33
+ disease_name = labels.get(predicted_class, "Unknown Disease")
 
 
34
 
35
+ return f"Disease Detected: {disease_name}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # Build Gradio Interface
38
+ title = "Tobacco Leaf Disease Detection"
39
+ description = """
40
+ Upload or take a real-time picture of a tobacco leaf, and the app will detect the disease (if any).
 
41
  """
42
 
43
+ # Create Gradio interface with camera and real-time processing
44
+ iface = gr.Interface(
45
+ fn=detect_disease,
46
+ inputs=gr.Image(source="camera", type="pil", tool="editor"),
47
+ outputs="text",
48
+ title=title,
49
+ description=description,
50
+ live=True # Enables real-time processing
51
+ )
52
 
53
+ # Launch Gradio app
54
+ iface.launch()