Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Ensure API key is available
|
| 11 |
+
if not api_key:
|
| 12 |
+
st.error("API key not found. Please set GROQ_API_KEY in the .env file.")
|
| 13 |
+
st.stop()
|
| 14 |
+
|
| 15 |
+
# Initialize Groq client
|
| 16 |
+
client = Groq(api_key=api_key)
|
| 17 |
+
|
| 18 |
+
# Function to get chatbot response
|
| 19 |
+
def get_chat_response(query):
|
| 20 |
+
chat_completion = client.chat.completions.create(
|
| 21 |
+
messages=[{"role": "user", "content": query}],
|
| 22 |
+
model="llama-3.3-70b-versatile",
|
| 23 |
+
)
|
| 24 |
+
return chat_completion.choices[0].message.content
|
| 25 |
+
|
| 26 |
+
# Streamlit app interface
|
| 27 |
+
def main():
|
| 28 |
+
st.title("Engineering Assistant Chatbot")
|
| 29 |
+
st.write("Ask any engineering-related questions, and I will provide solutions.")
|
| 30 |
+
|
| 31 |
+
user_input = st.text_input("Enter your engineering-related question:")
|
| 32 |
+
|
| 33 |
+
if user_input:
|
| 34 |
+
engineering_keywords = [
|
| 35 |
+
"mechanical", "electrical", "civil", "software", "chemical", "structural",
|
| 36 |
+
"thermodynamics", "fluid mechanics", "circuit", "power", "robotics", "AI",
|
| 37 |
+
"automation", "material science", "manufacturing", "electronics", "coding",
|
| 38 |
+
"design", "engineering problem", "mathematics", "physics", "statics", "dynamics"
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
if any(keyword in user_input.lower() for keyword in engineering_keywords):
|
| 42 |
+
response = get_chat_response(user_input)
|
| 43 |
+
st.write("Chatbot Response:", response)
|
| 44 |
+
else:
|
| 45 |
+
st.write("Sorry, I only answer engineering-related questions.")
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
main()
|