AhmadRAZA23 commited on
Commit
a4fad62
·
verified ·
1 Parent(s): 7d67564

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -2,11 +2,21 @@ import streamlit as st
2
  import requests
3
  from transformers import pipeline
4
 
5
- # Initialize the AI model for answering plant-related questions
6
- qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad", framework="pt")
7
- text_generation_pipeline = pipeline("text-generation", model="gpt2", framework="pt")
 
8
 
9
- # Function to fetch plant data using OpenFarm API
 
 
 
 
 
 
 
 
 
10
  def fetch_plant_data(plant_name):
11
  url = f"https://openfarm.cc/api/v1/crops?filter={plant_name}"
12
  response = requests.get(url)
 
2
  import requests
3
  from transformers import pipeline
4
 
5
+ # Cache the AI models to avoid reloading them on every interaction
6
+ @st.cache_resource
7
+ def load_qa_model():
8
+ return pipeline("question-answering", model="distilbert-base-cased-distilled-squad", framework="pt")
9
 
10
+ @st.cache_resource
11
+ def load_text_generation_model():
12
+ return pipeline("text-generation", model="gpt2", framework="pt")
13
+
14
+ # Initialize the AI models
15
+ qa_pipeline = load_qa_model()
16
+ text_generation_pipeline = load_text_generation_model()
17
+
18
+ # Cache the plant data fetching function to avoid redundant API calls
19
+ @st.cache_data(ttl=3600) # Cache for 1 hour
20
  def fetch_plant_data(plant_name):
21
  url = f"https://openfarm.cc/api/v1/crops?filter={plant_name}"
22
  response = requests.get(url)