Mahrukhh commited on
Commit
c283a07
Β·
verified Β·
1 Parent(s): d70fb92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -35
app.py CHANGED
@@ -5,46 +5,44 @@ import requests
5
  from PIL import Image
6
  from io import BytesIO
7
 
8
- # πŸ” Groq API Key
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "PASTE_YOUR_GROQ_API_KEY_HERE"
10
-
11
- # βœ… Model details
12
  GROQ_MODEL = "llama-3.3-70b-versatile"
13
  GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
14
 
15
- # 🧠 Chat history
16
  history = []
17
 
18
  def analyze_image_with_prompt(image, user_prompt):
19
  global history
20
 
21
  if not image or not user_prompt.strip():
22
- return "⚠️ Please upload an image and enter a question or prompt."
23
 
24
- # πŸ’‘ Only include user prompt β€” not base64 image
25
  if len(history) == 0:
26
- system_msg = {
27
  "role": "system",
28
  "content": (
29
- "You are a helpful construction engineer bot. "
30
- "Given a prompt describing a construction issue (e.g., crack, damage), identify:\n"
31
  "- Type of damage\n"
32
  "- Possible causes\n"
33
  "- Recommended tools and materials\n"
34
- "- Estimated repair time.\n"
35
- "Then answer follow-up questions."
36
  )
37
- }
38
- history.append(system_msg)
39
 
40
  # Add user message
41
  history.append({"role": "user", "content": user_prompt})
42
 
43
- # βœ… Limit chat history to avoid hitting token limits
44
- MAX_TURNS = 5 # 5 user-assistant exchanges
45
- if len(history) > (MAX_TURNS * 2 + 1): # +1 for system
46
  history = [history[0]] + history[-(MAX_TURNS * 2):]
47
 
 
48
  headers = {
49
  "Authorization": f"Bearer {GROQ_API_KEY}",
50
  "Content-Type": "application/json"
@@ -63,49 +61,66 @@ 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_history():
69
  global history
70
  history = []
71
  return "", ""
72
 
73
- # 🌟 Gradio GUI
74
- with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container { max-width: 950px; margin: auto; }") as demo:
75
- gr.Markdown("## πŸ—οΈ Construction Damage Identifier Chatbot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  gr.Markdown(
77
- "Upload a photo of construction damage (for reference) and ask a question. "
78
- "The AI will try to diagnose the issue based on your prompt."
79
  )
80
 
81
  with gr.Row():
82
- with gr.Column(scale=1):
83
  img_input = gr.Image(
84
  type="pil",
85
- label="πŸ–ΌοΈ Upload Image (for your reference)",
86
  height=300
87
  )
88
  prompt_input = gr.Textbox(
89
- lines=3,
90
- label="🧠 Your Prompt / Question",
91
- placeholder="e.g., What kind of crack is this? What caused it?"
 
92
  )
93
-
94
  with gr.Row():
95
- submit_btn = gr.Button("πŸ” Analyze", variant="primary")
96
- reset_btn = gr.Button("πŸ” New Session")
97
 
98
- with gr.Column(scale=1):
99
  output = gr.Textbox(
100
- label="πŸ’¬ Bot Response",
 
101
  lines=20,
102
  interactive=False,
103
  show_copy_button=True
104
  )
105
 
106
- submit_btn.click(analyze_image_with_prompt, inputs=[img_input, prompt_input], outputs=[output])
107
- reset_btn.click(reset_history, outputs=[output, prompt_input])
108
 
109
- gr.Markdown("➑️ You can continue chatting with the AI about the same issue.")
 
110
 
111
  demo.launch()
 
5
  from PIL import Image
6
  from io import BytesIO
7
 
8
+ # πŸ” API Key
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "PASTE_YOUR_GROQ_API_KEY_HERE"
 
 
10
  GROQ_MODEL = "llama-3.3-70b-versatile"
11
  GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
12
 
13
+ # 🧠 Chat history memory
14
  history = []
15
 
16
  def analyze_image_with_prompt(image, user_prompt):
17
  global history
18
 
19
  if not image or not user_prompt.strip():
20
+ return "⚠️ Please upload an image and enter your question or prompt."
21
 
22
+ # Add system message on first prompt
23
  if len(history) == 0:
24
+ history.append({
25
  "role": "system",
26
  "content": (
27
+ "You are a helpful and professional construction engineer bot. "
28
+ "When provided with a prompt describing a construction issue, analyze the situation and explain:\n"
29
  "- Type of damage\n"
30
  "- Possible causes\n"
31
  "- Recommended tools and materials\n"
32
+ "- Estimated repair time\n"
33
+ "Then answer any follow-up questions helpfully."
34
  )
35
+ })
 
36
 
37
  # Add user message
38
  history.append({"role": "user", "content": user_prompt})
39
 
40
+ # Limit history to last 5 turns (10 messages + system)
41
+ MAX_TURNS = 5
42
+ if len(history) > (MAX_TURNS * 2 + 1):
43
  history = [history[0]] + history[-(MAX_TURNS * 2):]
44
 
45
+ # Request setup
46
  headers = {
47
  "Authorization": f"Bearer {GROQ_API_KEY}",
48
  "Content-Type": "application/json"
 
61
  history.append({"role": "assistant", "content": reply})
62
  return reply
63
  else:
64
+ return f"❌ Error {response.status_code}: {response.text}"
65
 
66
  def reset_history():
67
  global history
68
  history = []
69
  return "", ""
70
 
71
+ # 🎨 Custom CSS
72
+ CUSTOM_CSS = """
73
+ .gradio-container {
74
+ max-width: 960px;
75
+ margin: auto;
76
+ padding-top: 1.5rem;
77
+ }
78
+ footer {
79
+ text-align: center;
80
+ font-size: 0.9rem;
81
+ margin-top: 2rem;
82
+ color: gray;
83
+ }
84
+ """
85
+
86
+ # πŸš€ Gradio App
87
+ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
88
+ gr.Markdown("### πŸ—οΈ Construction Damage Identifier Bot")
89
  gr.Markdown(
90
+ "Upload a construction site photo (e.g., cracks, corrosion) and ask a question. "
91
+ "This assistant will provide expert-level analysis based on your description."
92
  )
93
 
94
  with gr.Row():
95
+ with gr.Column(scale=1, min_width=320):
96
  img_input = gr.Image(
97
  type="pil",
98
+ label="πŸ–ΌοΈ Upload Damage Photo",
99
  height=300
100
  )
101
  prompt_input = gr.Textbox(
102
+ label="🧠 Enter Your Prompt",
103
+ placeholder="e.g., What kind of crack is this? What caused it?",
104
+ lines=4,
105
+ show_label=True
106
  )
 
107
  with gr.Row():
108
+ analyze_btn = gr.Button("πŸ” Analyze", variant="primary")
109
+ reset_btn = gr.Button("πŸ” Reset", variant="secondary")
110
 
111
+ with gr.Column(scale=1, min_width=320):
112
  output = gr.Textbox(
113
+ label="πŸ’¬ Expert Response",
114
+ placeholder="Response will appear here...",
115
  lines=20,
116
  interactive=False,
117
  show_copy_button=True
118
  )
119
 
120
+ analyze_btn.click(analyze_image_with_prompt, inputs=[img_input, prompt_input], outputs=output)
121
+ reset_btn.click(reset_history, outputs=[prompt_input, output])
122
 
123
+ gr.Markdown("⚠️ Note: The model doesn't directly analyze the image, but uses your question to assist.")
124
+ gr.HTML("<footer>Made for field engineers and civil professionals πŸ› οΈ</footer>")
125
 
126
  demo.launch()