sumitkrsharma commited on
Commit
856f57c
·
verified ·
1 Parent(s): 1ba3933

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import base64
4
+ from PIL import Image
5
+ import os
6
+
7
+ # 🔑 Get OpenAI key from Hugging Face Secrets
8
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
9
+ if not OPENAI_API_KEY:
10
+ raise ValueError("Please set OPENAI_API_KEY in Hugging Face Secrets")
11
+
12
+ openai.api_key = OPENAI_API_KEY
13
+
14
+ def encode_image(image_path):
15
+ """Convert uploaded image to base64 string"""
16
+ with open(image_path, "rb") as img_file:
17
+ return base64.b64encode(img_file.read()).decode("utf-8")
18
+
19
+ def classify_medicine(image_path):
20
+ try:
21
+ image_b64 = encode_image(image_path)
22
+
23
+ response = openai.chat.completions.create(
24
+ model="gpt-4o-mini",
25
+ messages=[
26
+ {"role": "system", "content": "You are an assistant that classifies medicine packaging."},
27
+ {
28
+ "role": "user",
29
+ "content": [
30
+ {"type": "text", "text": "Classify the uploaded medicine image as 'Generic' or 'Ethical (Branded)'. Only respond with one of these two labels."},
31
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}}
32
+ ]
33
+ }
34
+ ],
35
+ )
36
+
37
+ result = response.choices[0].message.content.strip()
38
+ return image_path, f"✅ Prediction: {result}"
39
+
40
+ except Exception as e:
41
+ return None, f"⚠️ Error: {str(e)}"
42
+
43
+ # 🎨 Custom UI
44
+ custom_css = """
45
+ .gradio-container {
46
+ background-color: #f9fdfb !important;
47
+ font-family: 'Segoe UI', sans-serif;
48
+ color: #003333 !important;
49
+ padding: 20px;
50
+ }
51
+ h1 {
52
+ color: #006666 !important;
53
+ text-align: center;
54
+ font-weight: 700;
55
+ margin-bottom: 10px;
56
+ }
57
+ p {
58
+ text-align: center;
59
+ color: #004d4d !important;
60
+ font-size: 16px;
61
+ }
62
+ .card {
63
+ background: white;
64
+ border-radius: 16px;
65
+ padding: 20px;
66
+ box-shadow: 0px 4px 15px rgba(0,0,0,0.08);
67
+ }
68
+ button {
69
+ background-color: #f9fdfb !important;
70
+ color: black !important;
71
+ border-radius: 8px !important;
72
+ font-weight: bold;
73
+ font-size: 15px;
74
+ padding: 10px 20px;
75
+ }
76
+ button:hover {
77
+ background-color: #f9fdfb !important;
78
+ }
79
+ """
80
+
81
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
82
+ gr.Markdown("<h1>💊 Pharma Medicine Classifier</h1><p>Upload a medicine image to check if it is <b>Generic</b> or <b>Ethical (Branded)</b>.</p>")
83
+
84
+ with gr.Row(elem_classes="card"):
85
+ with gr.Column():
86
+ input_img = gr.Image(type="filepath", label="📷 Upload Medicine Image", height=250)
87
+ classify_btn = gr.Button("🔍 Classify Medicine", variant="primary")
88
+ with gr.Column():
89
+ preview_img = gr.Image(label="Uploaded Image", interactive=False, height=250)
90
+ output_label = gr.Textbox(label="Result", interactive=False)
91
+
92
+ classify_btn.click(fn=classify_medicine, inputs=input_img, outputs=[preview_img, output_label])
93
+
94
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai
3
+ pillow