Mahrukhh commited on
Commit
14efa8c
Β·
verified Β·
1 Parent(s): 52254d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -5,12 +5,11 @@ import requests
5
  from PIL import Image
6
  from io import BytesIO
7
 
8
- # πŸ” Groq API key setup
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "PASTE_YOUR_GROQ_API_KEY_HERE"
10
  GROQ_MODEL = "llama3-8b-8192"
11
  GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
12
 
13
- # πŸ’¬ Session history
14
  history = []
15
 
16
  def analyze_image_with_prompt(image, user_prompt):
@@ -19,27 +18,22 @@ def analyze_image_with_prompt(image, user_prompt):
19
  if image is None or not user_prompt.strip():
20
  return "⚠️ Please upload an image and ask a question."
21
 
22
- # Encode image to base64 (placeholder, not used directly)
23
  buffered = BytesIO()
24
  image.save(buffered, format="JPEG")
25
  img_base64 = base64.b64encode(buffered.getvalue()).decode()
26
 
27
- # System role prompt (set only once)
28
  if not history:
29
  history.append({
30
  "role": "system",
31
  "content": (
32
- "You are a professional construction assistant.\n"
33
- "Users upload images and describe issues.\n"
34
- "Based on their description, provide:\n"
35
- "- The type of construction damage\n"
36
  "- Possible causes\n"
37
  "- Recommended tools/materials\n"
38
  "- Estimated repair time"
39
  )
40
  })
41
 
42
- # Add user message with image context
43
  history.append({
44
  "role": "user",
45
  "content": f"Image uploaded. Description: {user_prompt}"
@@ -63,43 +57,56 @@ def analyze_image_with_prompt(image, user_prompt):
63
  history.append({"role": "assistant", "content": reply})
64
  return reply
65
  else:
66
- return f"❌ Error: {response.status_code}\n{response.text}"
67
 
68
  def reset_session():
69
  global history
70
  history = []
71
- return "βœ… Session reset. You can upload a new image and ask a question."
72
-
73
- # 🎨 Gradio App
74
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
75
- gr.Markdown("## πŸ—οΈ Construction Assistant: Upload Image & Ask a Question")
76
-
77
- with gr.Row():
78
- with gr.Column(scale=1):
79
- img_input = gr.Image(
80
- type="pil",
81
- label="πŸ“€ Upload a Picture of the Problem"
82
- )
83
-
84
- prompt_input = gr.Textbox(
85
- lines=3,
86
- label="πŸ“ Describe the Problem or Ask a Question",
87
- placeholder="e.g. What caused this crack? How can I repair it?"
88
- )
89
-
90
- with gr.Row():
91
- submit_btn = gr.Button("πŸ” Get Analysis", variant="primary")
92
- reset_btn = gr.Button("πŸ” Start Over")
93
-
94
- with gr.Column(scale=1):
95
- output = gr.Textbox(
96
- label="🧠 Assistant’s Response",
97
- lines=20,
98
- show_copy_button=True
99
- )
100
-
101
- submit_btn.click(analyze_image_with_prompt, inputs=[img_input, prompt_input], outputs=[output])
102
- reset_btn.click(reset_session, outputs=[output])
103
-
104
- # πŸš€ Launch
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  demo.launch()
 
5
  from PIL import Image
6
  from io import BytesIO
7
 
8
+ # Set your Groq API Key
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "PASTE_YOUR_GROQ_API_KEY_HERE"
10
  GROQ_MODEL = "llama3-8b-8192"
11
  GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
12
 
 
13
  history = []
14
 
15
  def analyze_image_with_prompt(image, user_prompt):
 
18
  if image is None or not user_prompt.strip():
19
  return "⚠️ Please upload an image and ask a question."
20
 
 
21
  buffered = BytesIO()
22
  image.save(buffered, format="JPEG")
23
  img_base64 = base64.b64encode(buffered.getvalue()).decode()
24
 
 
25
  if not history:
26
  history.append({
27
  "role": "system",
28
  "content": (
29
+ "You are a professional construction assistant. Users upload images and describe damage. Based on the description, provide:\n"
30
+ "- Type of construction damage\n"
 
 
31
  "- Possible causes\n"
32
  "- Recommended tools/materials\n"
33
  "- Estimated repair time"
34
  )
35
  })
36
 
 
37
  history.append({
38
  "role": "user",
39
  "content": f"Image uploaded. Description: {user_prompt}"
 
57
  history.append({"role": "assistant", "content": reply})
58
  return reply
59
  else:
60
+ return f"\u274c Error: {response.status_code}\n{response.text}"
61
 
62
  def reset_session():
63
  global history
64
  history = []
65
+ return "βœ… Session reset. Please upload a new image and ask a question."
66
+
67
+ # UI Design
68
+ with gr.Blocks(theme=gr.themes.Base(), css="""
69
+ #main-box {background-color: #f4f4f9; border-radius: 20px; padding: 2rem; box-shadow: 0 4px 12px rgba(0,0,0,0.1);}
70
+ h1, h2, h3 {color: #2c3e50;}
71
+ .gr-button {border-radius: 10px !important; font-weight: bold; font-size: 1rem;}
72
+ .gr-textbox textarea {font-size: 1rem;}
73
+ .gr-textbox label {font-weight: bold; color: #34495e;}
74
+ .gr-image label {font-weight: bold; color: #34495e;}
75
+ """) as demo:
76
+
77
+ with gr.Column(elem_id="main-box"):
78
+ gr.Markdown("""
79
+ # πŸ—οΈ Construction Damage Assistant
80
+ Upload a picture and describe the issue to get detailed construction repair guidance.
81
+ """)
82
+
83
+ with gr.Row():
84
+ with gr.Column(scale=1):
85
+ img_input = gr.Image(
86
+ type="pil",
87
+ label="πŸ“€ Upload Picture of Damage",
88
+ height=300
89
+ )
90
+
91
+ prompt_input = gr.Textbox(
92
+ lines=4,
93
+ label="πŸ“ Ask Your Question or Describe the Problem",
94
+ placeholder="e.g., What caused this crack? How to fix it?"
95
+ )
96
+
97
+ with gr.Row():
98
+ submit_btn = gr.Button("πŸ” Analyze Issue", variant="primary")
99
+ reset_btn = gr.Button("πŸ” Start New Session")
100
+
101
+ with gr.Column(scale=1):
102
+ output = gr.Textbox(
103
+ label="🧠 AI Assistant's Response",
104
+ lines=20,
105
+ show_copy_button=True
106
+ )
107
+
108
+ submit_btn.click(analyze_image_with_prompt, inputs=[img_input, prompt_input], outputs=[output])
109
+ reset_btn.click(reset_session, outputs=[output])
110
+
111
+ # Launch the app
112
  demo.launch()