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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -5,14 +5,14 @@ import requests
5
  from PIL import Image
6
  from io import BytesIO
7
 
8
- # πŸ” Groq API Key (Set via environment variable or paste manually for local testing)
9
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "PASTE_YOUR_GROQ_API_KEY_HERE"
10
 
11
- # βœ… Groq model info
12
  GROQ_MODEL = "llama-3.3-70b-versatile"
13
  GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
14
 
15
- # 🧠 Chat session memory
16
  history = []
17
 
18
  def analyze_image_with_prompt(image, user_prompt):
@@ -21,18 +21,13 @@ def analyze_image_with_prompt(image, user_prompt):
21
  if not image or not user_prompt.strip():
22
  return "⚠️ Please upload an image and enter a question or prompt."
23
 
24
- # Convert image to base64
25
- buffered = BytesIO()
26
- image.save(buffered, format="JPEG")
27
- img_base64 = base64.b64encode(buffered.getvalue()).decode()
28
-
29
- # Add system prompt if first message
30
  if len(history) == 0:
31
  system_msg = {
32
  "role": "system",
33
  "content": (
34
  "You are a helpful construction engineer bot. "
35
- "Given an image of a construction issue and a prompt, identify:\n"
36
  "- Type of damage\n"
37
  "- Possible causes\n"
38
  "- Recommended tools and materials\n"
@@ -41,13 +36,14 @@ def analyze_image_with_prompt(image, user_prompt):
41
  )
42
  }
43
  history.append(system_msg)
44
- user_msg = {
45
- "role": "user",
46
- "content": f"Image base64: {img_base64}\nPrompt: {user_prompt}"
47
- }
48
- history.append(user_msg)
49
- else:
50
- history.append({"role": "user", "content": user_prompt})
 
51
 
52
  headers = {
53
  "Authorization": f"Bearer {GROQ_API_KEY}",
@@ -78,15 +74,15 @@ def reset_history():
78
  with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container { max-width: 950px; margin: auto; }") as demo:
79
  gr.Markdown("## πŸ—οΈ Construction Damage Identifier Chatbot")
80
  gr.Markdown(
81
- "Upload a photo of construction damage (e.g., cracks, corrosion) and ask a question. "
82
- "The AI will try to diagnose the problem and suggest solutions."
83
  )
84
 
85
  with gr.Row():
86
  with gr.Column(scale=1):
87
  img_input = gr.Image(
88
  type="pil",
89
- label="πŸ–ΌοΈ Upload Image of Damage",
90
  height=300
91
  )
92
  prompt_input = gr.Textbox(
@@ -110,7 +106,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container { max-width: 950px
110
  submit_btn.click(analyze_image_with_prompt, inputs=[img_input, prompt_input], outputs=[output])
111
  reset_btn.click(reset_history, outputs=[output, prompt_input])
112
 
113
- gr.Markdown("➑️ You can continue chatting with the AI about the same image.")
114
 
115
- # πŸš€ Launch app
116
  demo.launch()
 
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):
 
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"
 
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}",
 
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(
 
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()