Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
BASE_API_URL = "https://api.langflow.astra.datastax.com"
|
| 10 |
+
LANGFLOW_ID = "478dd003-8ac0-4d40-815e-77e7d1ae9343"
|
| 11 |
+
FLOW_ID = "0853dfd7-558c-4958-9ca4-dc9ca8c69302"
|
| 12 |
+
APPLICATION_TOKEN = os.environ.get("APP_TOKEN")
|
| 13 |
+
ENDPOINT = "materialssciencebot" # You can set a specific endpoint name in the flow settings
|
| 14 |
+
|
| 15 |
+
def run_flow(message: str) -> dict:
|
| 16 |
+
api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{ENDPOINT}"
|
| 17 |
+
|
| 18 |
+
payload = {
|
| 19 |
+
"input_value": message,
|
| 20 |
+
"output_type": "chat",
|
| 21 |
+
"input_type": "chat",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
headers = {"Authorization": "Bearer " + APPLICATION_TOKEN, "Content-Type": "application/json"}
|
| 25 |
+
response = requests.post(api_url, json=payload, headers=headers)
|
| 26 |
+
return response.json()
|
| 27 |
+
|
| 28 |
+
# Adding an image at the top
|
| 29 |
+
st.image(r"C:/Users/topic/OneDrive/Pictures/WhatsApp Image 2025-02-10 at 16.18.20_a1254462.jpg", use_container_width=True)
|
| 30 |
+
|
| 31 |
+
st.markdown("""
|
| 32 |
+
<style>
|
| 33 |
+
.footer {
|
| 34 |
+
position: fixed;
|
| 35 |
+
bottom: 0;
|
| 36 |
+
right: 0;
|
| 37 |
+
padding: 10px;
|
| 38 |
+
font-size: 16px;
|
| 39 |
+
color: #333;
|
| 40 |
+
background-color: #f1f1f1;
|
| 41 |
+
}
|
| 42 |
+
</style>
|
| 43 |
+
<div class="footer">
|
| 44 |
+
Made with ❤️ by Baibhav Malviya
|
| 45 |
+
</div>
|
| 46 |
+
""", unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
def main():
|
| 49 |
+
st.title("Materials Science Bot")
|
| 50 |
+
st.markdown("<h4 style='font-size: 20px;'> Ask anything related to the world of materials! 😉</h4>", unsafe_allow_html=True)
|
| 51 |
+
message = st.text_area("Message", placeholder="What is oxidation?...")
|
| 52 |
+
|
| 53 |
+
if st.button("Run"):
|
| 54 |
+
if not message.strip():
|
| 55 |
+
st.error("Please enter a message")
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
with st.spinner("Running flow..."):
|
| 60 |
+
response = run_flow(message)
|
| 61 |
+
|
| 62 |
+
response = response['outputs'][0]['outputs'][0]['results']['message']['text']
|
| 63 |
+
st.markdown(response)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(str(e))
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|