Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,37 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import base64
|
| 4 |
import requests
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
# Set your Groq API
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
def analyze_image_with_prompt(image, user_prompt):
|
| 16 |
-
global history
|
| 17 |
-
|
| 18 |
if image is None or not user_prompt.strip():
|
| 19 |
-
return "β οΈ Please upload an image and
|
| 20 |
|
|
|
|
| 21 |
buffered = BytesIO()
|
| 22 |
image.save(buffered, format="JPEG")
|
| 23 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"- Estimated repair time"
|
| 34 |
-
)
|
| 35 |
-
})
|
| 36 |
|
| 37 |
-
|
| 38 |
-
"role": "
|
| 39 |
-
"content": f"Image uploaded
|
| 40 |
-
|
| 41 |
|
| 42 |
headers = {
|
| 43 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -46,67 +40,53 @@ def analyze_image_with_prompt(image, user_prompt):
|
|
| 46 |
|
| 47 |
payload = {
|
| 48 |
"model": GROQ_MODEL,
|
| 49 |
-
"messages":
|
| 50 |
"temperature": 0.7
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
reply = response.json()["choices"][0]["message"]["content"]
|
| 57 |
-
history.append({"role": "assistant", "content": reply})
|
| 58 |
return reply
|
| 59 |
-
|
| 60 |
-
return f"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
.
|
| 74 |
-
|
| 75 |
-
""")
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 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()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import requests
|
| 4 |
+
import base64
|
| 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_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
+
GROQ_MODEL = "llama3-70b-8192"
|
| 12 |
|
| 13 |
+
def analyze_damage(image, user_prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if image is None or not user_prompt.strip():
|
| 15 |
+
return "β οΈ Please upload an image and enter a question or description."
|
| 16 |
|
| 17 |
+
# Convert image to base64 for potential future use
|
| 18 |
buffered = BytesIO()
|
| 19 |
image.save(buffered, format="JPEG")
|
| 20 |
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 21 |
|
| 22 |
+
system_prompt = (
|
| 23 |
+
"You are a helpful construction engineer. The user has uploaded an image of some construction damage "
|
| 24 |
+
"and has asked a question. Based on the image and their description or question, identify:\n"
|
| 25 |
+
"- Type of damage\n"
|
| 26 |
+
"- Possible causes\n"
|
| 27 |
+
"- Recommended repair materials/tools\n"
|
| 28 |
+
"- Estimated repair time"
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
messages = [
|
| 32 |
+
{"role": "system", "content": system_prompt},
|
| 33 |
+
{"role": "user", "content": f"Image uploaded.\nQuestion: {user_prompt}"}
|
| 34 |
+
]
|
| 35 |
|
| 36 |
headers = {
|
| 37 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 40 |
|
| 41 |
payload = {
|
| 42 |
"model": GROQ_MODEL,
|
| 43 |
+
"messages": messages,
|
| 44 |
"temperature": 0.7
|
| 45 |
}
|
| 46 |
|
| 47 |
+
try:
|
| 48 |
+
response = requests.post(GROQ_URL, headers=headers, json=payload)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
reply = response.json()["choices"][0]["message"]["content"]
|
|
|
|
| 51 |
return reply
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"β Error: {str(e)}"
|
| 54 |
+
|
| 55 |
+
# Gradio UI
|
| 56 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as demo:
|
| 57 |
+
gr.Markdown("# ποΈ Construction Damage Assistant")
|
| 58 |
+
|
| 59 |
+
gr.Markdown("""
|
| 60 |
+
Upload an image of the damage and ask a question like:<br>
|
| 61 |
+
πΉ *What type of crack is this?*<br>
|
| 62 |
+
οΏ½οΏ½οΏ½ *What material should I use to fix it?*<br><br>
|
| 63 |
+
The assistant will analyze your question and give expert suggestions.
|
| 64 |
+
""", elem_classes="description-box")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column(scale=1):
|
| 68 |
+
image_input = gr.Image(label="πΈ Upload Damage Photo", type="pil", height=280)
|
| 69 |
+
|
| 70 |
+
question_input = gr.Textbox(
|
| 71 |
+
label="π§Ύ Ask a Question or Describe the Problem",
|
| 72 |
+
placeholder="e.g., What caused this crack? How do I fix it?",
|
| 73 |
+
lines=3
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
submit_btn = gr.Button("π Analyze", variant="primary")
|
| 77 |
+
clear_btn = gr.Button("π§Ή Clear Inputs")
|
| 78 |
+
|
| 79 |
+
with gr.Column(scale=1.5):
|
| 80 |
+
output = gr.Textbox(
|
| 81 |
+
label="π‘ AI Response",
|
| 82 |
+
lines=18,
|
| 83 |
+
show_copy_button=True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
submit_btn.click(fn=analyze_damage, inputs=[image_input, question_input], outputs=output)
|
| 87 |
+
clear_btn.click(lambda: (None, "", ""), outputs=[image_input, question_input, output])
|
| 88 |
+
|
| 89 |
+
gr.Markdown("___")
|
| 90 |
+
gr.Markdown("π§ Powered by **Groq LLaMA3** | Built with β€οΈ using **Gradio**")
|
| 91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
demo.launch()
|