Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,14 @@ 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 |
-
# β
|
| 12 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 13 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 14 |
|
| 15 |
-
# π§ Chat
|
| 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 |
-
#
|
| 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
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 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 (
|
| 82 |
-
"The AI will try to diagnose the
|
| 83 |
)
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column(scale=1):
|
| 87 |
img_input = gr.Image(
|
| 88 |
type="pil",
|
| 89 |
-
label="πΌοΈ Upload Image
|
| 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
|
| 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()
|