Mujtaba29 commited on
Commit
23eb8aa
Β·
verified Β·
1 Parent(s): 9722f70

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from groq import Groq
4
+
5
+ # Set the Groq API key
6
+ os.environ["GROQ_API_KEY"] = "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"
7
+
8
+ # Initialize Groq client
9
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
+
11
+ # Carbon footprint reduction data (kg CO2 per kg recycled)
12
+ carbon_reduction_data = {
13
+ "Plastic Bottles": 3.8,
14
+ "Glass Bottles": 0.5,
15
+ "Metal Cans": 9.0,
16
+ "Old Clothes": 2.0,
17
+ "Paper and Cardboard": 1.3,
18
+ "E-Waste": 15.0,
19
+ "Tires": 8.0,
20
+ }
21
+
22
+ # Function to call Groq LLM
23
+ def get_recycling_suggestions_from_groq(item, quantity):
24
+ prompt = (
25
+ f"You are an expert in recycling and sustainability. "
26
+ f"Suggest profitable and eco-friendly uses for {quantity} kg of {item}, "
27
+ f"including household uses, ways to monetize them, and calculate carbon footprint reduction."
28
+ )
29
+ chat_completion = client.chat.completions.create(
30
+ messages=[{"role": "user", "content": prompt}],
31
+ model="llama-3.3-70b-versatile",
32
+ stream=False,
33
+ )
34
+ return chat_completion.choices[0].message.content
35
+
36
+ # App title
37
+ st.title("♻️ Recycle with Groq LLM 🌍")
38
+ st.write("Select clutter items, specify quantities, and get tailored, profitable recycling suggestions along with carbon footprint reduction scores!")
39
+
40
+ # Multi-select input for clutter items
41
+ selected_items = st.multiselect(
42
+ "Select items to recycle:",
43
+ list(carbon_reduction_data.keys())
44
+ )
45
+
46
+ # Quantity input for selected items
47
+ quantities = {}
48
+ for item in selected_items:
49
+ quantities[item] = st.number_input(
50
+ f"Enter quantity for {item} (in kg):", min_value=0, step=1
51
+ )
52
+
53
+ # Process and display results
54
+ if st.button("Get Recycling Suggestions"):
55
+ if selected_items:
56
+ total_carbon_reduction = 0
57
+ st.write("### ♻️ Recycling Suggestions and Impact:")
58
+ for item, quantity in quantities.items():
59
+ if quantity > 0:
60
+ # Call Groq LLM for dynamic suggestions
61
+ llm_response = get_recycling_suggestions_from_groq(item, quantity)
62
+
63
+ # Fetch carbon footprint reduction
64
+ carbon_reduction = carbon_reduction_data.get(item, 0) * quantity
65
+ total_carbon_reduction += carbon_reduction
66
+
67
+ # Display results for each item
68
+ st.write(f"**{item} ({quantity} kg)**")
69
+ st.write(llm_response)
70
+ st.write(f"🌍 **Carbon Footprint Reduction**: {carbon_reduction:.2f} kg COβ‚‚")
71
+ st.write("---")
72
+
73
+ # Display total carbon footprint reduction credit score
74
+ st.write("### 🌟 Your Total Carbon Footprint Reduction 🌟")
75
+ st.write(f"🌍 **{total_carbon_reduction:.2f} kg COβ‚‚ saved**")
76
+ st.success("Great job contributing to a greener planet! πŸŒ±πŸ’š")
77
+ else:
78
+ st.error("Please select at least one item and specify its quantity.")
79
+
80
+ # Follow-up Q&A with Groq LLM
81
+ st.write("### πŸ€” Have more questions about recycling?")
82
+ user_query = st.text_input("Ask the Groq LLM about recycling:")
83
+ if st.button("Ask Groq"):
84
+ if user_query:
85
+ follow_up_response = client.chat.completions.create(
86
+ messages=[{"role": "user", "content": user_query}],
87
+ model="llama-3.3-70b-versatile",
88
+ stream=False,
89
+ ).choices[0].message.content
90
+ st.write("### 🧠 Groq LLM's Answer:")
91
+ st.write(follow_up_response)
92
+ else:
93
+ st.error("Please enter a question.")