Update app.py
Browse files
app.py
CHANGED
|
@@ -5,46 +5,44 @@ import requests
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
# π
|
| 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
|
| 23 |
|
| 24 |
-
#
|
| 25 |
if len(history) == 0:
|
| 26 |
-
|
| 27 |
"role": "system",
|
| 28 |
"content": (
|
| 29 |
-
"You are a helpful construction engineer bot. "
|
| 30 |
-
"
|
| 31 |
"- Type of damage\n"
|
| 32 |
"- Possible causes\n"
|
| 33 |
"- Recommended tools and materials\n"
|
| 34 |
-
"- Estimated repair time
|
| 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 |
-
#
|
| 44 |
-
MAX_TURNS = 5
|
| 45 |
-
if len(history) > (MAX_TURNS * 2 + 1):
|
| 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
|
| 67 |
|
| 68 |
def reset_history():
|
| 69 |
global history
|
| 70 |
history = []
|
| 71 |
return "", ""
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
gr.Markdown(
|
| 77 |
-
"Upload a
|
| 78 |
-
"
|
| 79 |
)
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
-
with gr.Column(scale=1):
|
| 83 |
img_input = gr.Image(
|
| 84 |
type="pil",
|
| 85 |
-
label="πΌοΈ Upload
|
| 86 |
height=300
|
| 87 |
)
|
| 88 |
prompt_input = gr.Textbox(
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
)
|
| 93 |
-
|
| 94 |
with gr.Row():
|
| 95 |
-
|
| 96 |
-
reset_btn = gr.Button("π
|
| 97 |
|
| 98 |
-
with gr.Column(scale=1):
|
| 99 |
output = gr.Textbox(
|
| 100 |
-
label="π¬
|
|
|
|
| 101 |
lines=20,
|
| 102 |
interactive=False,
|
| 103 |
show_copy_button=True
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
reset_btn.click(reset_history, outputs=[
|
| 108 |
|
| 109 |
-
gr.Markdown("
|
|
|
|
| 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()
|