Spaces:
Sleeping
Sleeping
Commit
·
f08ec97
1
Parent(s):
8a7b78f
Updated
Browse files
app.py
CHANGED
|
@@ -69,8 +69,6 @@ class VectorRequest(BaseModel):
|
|
| 69 |
# ==============================
|
| 70 |
# MODELS PER ENDPOINT (Meta Models, Conversational)
|
| 71 |
# ==============================
|
| 72 |
-
|
| 73 |
-
# 1. Crop Doctor
|
| 74 |
crop_template = PromptTemplate(
|
| 75 |
input_variables=["symptoms"],
|
| 76 |
template="You are AgriCopilot, a multilingual AI assistant created to support farmers. Farmer reports: {symptoms}. Diagnose the most likely disease and suggest treatments in simple farmer-friendly language."
|
|
@@ -85,7 +83,6 @@ crop_llm = HuggingFaceEndpoint(
|
|
| 85 |
max_new_tokens=1024
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# 2. Multilingual Chat
|
| 89 |
chat_template = PromptTemplate(
|
| 90 |
input_variables=["query"],
|
| 91 |
template="You are AgriCopilot, a supportive multilingual AI guide built for farmers. Farmer says: {query}"
|
|
@@ -100,7 +97,6 @@ chat_llm = HuggingFaceEndpoint(
|
|
| 100 |
max_new_tokens=1024
|
| 101 |
)
|
| 102 |
|
| 103 |
-
# 3. Disaster Summarizer
|
| 104 |
disaster_template = PromptTemplate(
|
| 105 |
input_variables=["report"],
|
| 106 |
template="You are AgriCopilot, an AI disaster-response assistant. Summarize in simple steps: {report}"
|
|
@@ -115,7 +111,6 @@ disaster_llm = HuggingFaceEndpoint(
|
|
| 115 |
max_new_tokens=1024
|
| 116 |
)
|
| 117 |
|
| 118 |
-
# 4. Marketplace Recommendation
|
| 119 |
market_template = PromptTemplate(
|
| 120 |
input_variables=["product"],
|
| 121 |
template="You are AgriCopilot, an AI agricultural marketplace advisor. Farmer wants to sell or buy: {product}. Suggest best options and advice."
|
|
@@ -134,8 +129,8 @@ market_llm = HuggingFaceEndpoint(
|
|
| 134 |
# ENDPOINT HELPERS
|
| 135 |
# ==============================
|
| 136 |
def run_conversational_model(model, prompt: str):
|
| 137 |
-
"""Wraps prompt into HF conversational format"""
|
| 138 |
-
|
| 139 |
"inputs": {
|
| 140 |
"past_user_inputs": [],
|
| 141 |
"generated_responses": [],
|
|
@@ -143,6 +138,14 @@ def run_conversational_model(model, prompt: str):
|
|
| 143 |
}
|
| 144 |
})
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# ==============================
|
| 147 |
# ENDPOINTS
|
| 148 |
# ==============================
|
|
@@ -152,7 +155,7 @@ async def crop_doctor(req: CropRequest, authorization: str | None = Header(None)
|
|
| 152 |
prompt = crop_template.format(symptoms=req.symptoms)
|
| 153 |
try:
|
| 154 |
response = run_conversational_model(crop_llm, prompt)
|
| 155 |
-
return {"diagnosis":
|
| 156 |
except HfHubHTTPError as e:
|
| 157 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 158 |
|
|
@@ -162,7 +165,7 @@ async def multilingual_chat(req: ChatRequest, authorization: str | None = Header
|
|
| 162 |
prompt = chat_template.format(query=req.query)
|
| 163 |
try:
|
| 164 |
response = run_conversational_model(chat_llm, prompt)
|
| 165 |
-
return {"reply":
|
| 166 |
except HfHubHTTPError as e:
|
| 167 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 168 |
|
|
@@ -172,7 +175,7 @@ async def disaster_summarizer(req: DisasterRequest, authorization: str | None =
|
|
| 172 |
prompt = disaster_template.format(report=req.report)
|
| 173 |
try:
|
| 174 |
response = run_conversational_model(disaster_llm, prompt)
|
| 175 |
-
return {"summary":
|
| 176 |
except HfHubHTTPError as e:
|
| 177 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 178 |
|
|
@@ -182,7 +185,7 @@ async def marketplace(req: MarketRequest, authorization: str | None = Header(Non
|
|
| 182 |
prompt = market_template.format(product=req.product)
|
| 183 |
try:
|
| 184 |
response = run_conversational_model(market_llm, prompt)
|
| 185 |
-
return {"recommendation":
|
| 186 |
except HfHubHTTPError as e:
|
| 187 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 188 |
|
|
|
|
| 69 |
# ==============================
|
| 70 |
# MODELS PER ENDPOINT (Meta Models, Conversational)
|
| 71 |
# ==============================
|
|
|
|
|
|
|
| 72 |
crop_template = PromptTemplate(
|
| 73 |
input_variables=["symptoms"],
|
| 74 |
template="You are AgriCopilot, a multilingual AI assistant created to support farmers. Farmer reports: {symptoms}. Diagnose the most likely disease and suggest treatments in simple farmer-friendly language."
|
|
|
|
| 83 |
max_new_tokens=1024
|
| 84 |
)
|
| 85 |
|
|
|
|
| 86 |
chat_template = PromptTemplate(
|
| 87 |
input_variables=["query"],
|
| 88 |
template="You are AgriCopilot, a supportive multilingual AI guide built for farmers. Farmer says: {query}"
|
|
|
|
| 97 |
max_new_tokens=1024
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
disaster_template = PromptTemplate(
|
| 101 |
input_variables=["report"],
|
| 102 |
template="You are AgriCopilot, an AI disaster-response assistant. Summarize in simple steps: {report}"
|
|
|
|
| 111 |
max_new_tokens=1024
|
| 112 |
)
|
| 113 |
|
|
|
|
| 114 |
market_template = PromptTemplate(
|
| 115 |
input_variables=["product"],
|
| 116 |
template="You are AgriCopilot, an AI agricultural marketplace advisor. Farmer wants to sell or buy: {product}. Suggest best options and advice."
|
|
|
|
| 129 |
# ENDPOINT HELPERS
|
| 130 |
# ==============================
|
| 131 |
def run_conversational_model(model, prompt: str):
|
| 132 |
+
"""Wraps prompt into HF conversational format and extracts text"""
|
| 133 |
+
result = model.invoke({
|
| 134 |
"inputs": {
|
| 135 |
"past_user_inputs": [],
|
| 136 |
"generated_responses": [],
|
|
|
|
| 138 |
}
|
| 139 |
})
|
| 140 |
|
| 141 |
+
if isinstance(result, dict) and "generated_text" in result:
|
| 142 |
+
return result["generated_text"]
|
| 143 |
+
|
| 144 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
| 145 |
+
return result[0]["generated_text"]
|
| 146 |
+
|
| 147 |
+
return str(result) # fallback
|
| 148 |
+
|
| 149 |
# ==============================
|
| 150 |
# ENDPOINTS
|
| 151 |
# ==============================
|
|
|
|
| 155 |
prompt = crop_template.format(symptoms=req.symptoms)
|
| 156 |
try:
|
| 157 |
response = run_conversational_model(crop_llm, prompt)
|
| 158 |
+
return {"diagnosis": response}
|
| 159 |
except HfHubHTTPError as e:
|
| 160 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 161 |
|
|
|
|
| 165 |
prompt = chat_template.format(query=req.query)
|
| 166 |
try:
|
| 167 |
response = run_conversational_model(chat_llm, prompt)
|
| 168 |
+
return {"reply": response}
|
| 169 |
except HfHubHTTPError as e:
|
| 170 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 171 |
|
|
|
|
| 175 |
prompt = disaster_template.format(report=req.report)
|
| 176 |
try:
|
| 177 |
response = run_conversational_model(disaster_llm, prompt)
|
| 178 |
+
return {"summary": response}
|
| 179 |
except HfHubHTTPError as e:
|
| 180 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 181 |
|
|
|
|
| 185 |
prompt = market_template.format(product=req.product)
|
| 186 |
try:
|
| 187 |
response = run_conversational_model(market_llm, prompt)
|
| 188 |
+
return {"recommendation": response}
|
| 189 |
except HfHubHTTPError as e:
|
| 190 |
return {"error": f"HuggingFace error: {str(e)}"}
|
| 191 |
|