Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,66 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
|
| 3 |
-
load_dotenv() ## load all the environment variables
|
| 4 |
-
|
| 5 |
import streamlit as st
|
|
|
|
| 6 |
import os
|
| 7 |
-
import google.generativeai as genai
|
| 8 |
-
from PIL import Image
|
| 9 |
-
|
| 10 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
{
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
}
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
##initialize our streamlit app
|
| 36 |
-
|
| 37 |
-
st.set_page_config(page_title="Crop Disease Detection App")
|
| 38 |
-
|
| 39 |
-
st.header("Gemini Crop Disease App")
|
| 40 |
-
input=st.text_input("Input Prompt: ",key="input")
|
| 41 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 42 |
-
image=""
|
| 43 |
-
if uploaded_file is not None:
|
| 44 |
-
image = Image.open(uploaded_file)
|
| 45 |
-
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 46 |
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
"2. Whether it is infected or healthy, "
|
| 55 |
-
"3. Type of disease (if any), "
|
| 56 |
-
"4. How confident out of 100% whether image is healthy or infected "
|
| 57 |
-
"5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc., "
|
| 58 |
-
"6. Precautions for it."
|
| 59 |
-
"""
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 7 |
|
| 8 |
+
def generate_tutor_output(subject, difficulty, student_input):
|
| 9 |
+
prompt = f"""
|
| 10 |
+
You are an expert tutor in {subject} at the {difficulty} level.
|
| 11 |
+
The student has provided the following input: "{student_input}"
|
| 12 |
+
|
| 13 |
+
Please generate:
|
| 14 |
+
1. A brief, engaging lesson on the topic (2-3 paragraphs)
|
| 15 |
+
2. A thought-provoking question to check understanding
|
| 16 |
+
3. Constructive feedback on the student's input
|
| 17 |
+
|
| 18 |
+
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
| 19 |
+
"""
|
| 20 |
|
| 21 |
+
completion = client.chat.completions.create(
|
| 22 |
+
messages=[
|
| 23 |
+
{
|
| 24 |
+
"role": "system",
|
| 25 |
+
"content": "You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
|
| 26 |
+
},
|
| 27 |
{
|
| 28 |
+
"role": "user",
|
| 29 |
+
"content": prompt,
|
| 30 |
}
|
| 31 |
+
],
|
| 32 |
+
model="llama3-groq-70b-8192-tool-use-preview",
|
| 33 |
+
max_tokens=1000,
|
| 34 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return completion.choices[0].message.content
|
| 37 |
|
| 38 |
+
st.title("🎓 Your AI Tutor by Farhan")
|
| 39 |
|
| 40 |
+
# Subject selection
|
| 41 |
+
subject = st.selectbox("Subject", ["Math", "Science", "History", "Literature", "Code", "AI"], help="Choose the subject of your lesson")
|
| 42 |
+
difficulty = st.radio("Difficulty Level", ["Beginner", "Intermediate", "Advanced"], help="Select your proficiency level")
|
| 43 |
+
student_input = st.text_input("Your Input", placeholder="Type your query here...", help="Enter the topic you want to learn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
if st.button("Generate Lesson"):
|
| 46 |
+
output = generate_tutor_output(subject, difficulty, student_input)
|
| 47 |
+
try:
|
| 48 |
+
parsed = eval(output) # Use `json.loads` if your output is a valid JSON string
|
| 49 |
+
st.subheader("Lesson")
|
| 50 |
+
st.markdown(parsed["lesson"])
|
| 51 |
+
st.subheader("Comprehension Question")
|
| 52 |
+
st.markdown(parsed["question"])
|
| 53 |
+
st.subheader("Feedback")
|
| 54 |
+
st.markdown(parsed["feedback"])
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Error parsing output: {e}")
|
| 57 |
|
| 58 |
+
st.markdown("""
|
| 59 |
+
### How to Use
|
| 60 |
+
1. Select a subject from the dropdown.
|
| 61 |
+
2. Choose your difficulty level.
|
| 62 |
+
3. Enter the topic or question you'd like to explore.
|
| 63 |
+
4. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
|
| 64 |
+
5. Review the AI-generated content to enhance your learning.
|
| 65 |
+
6. Feel free to ask follow-up questions or explore new topics!
|
| 66 |
+
""")
|