Spaces:
Sleeping
Sleeping
Update answer_generation.py
Browse files- answer_generation.py +162 -162
answer_generation.py
CHANGED
|
@@ -1,169 +1,169 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
import google.generativeai as genai
|
| 6 |
-
from fastapi import FastAPI, HTTPException
|
| 7 |
import uvicorn
|
| 8 |
-
from prompts import PROMPTS
|
| 9 |
-
from llm_pipeline import example_odia_answer_json, example_odia_question_json
|
| 10 |
-
|
| 11 |
-
# Setup
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
# Check for required environment variables
|
| 15 |
-
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 16 |
-
if not google_api_key:
|
| 17 |
-
raise ValueError("GOOGLE_API_KEY not found in environment variables")
|
| 18 |
-
|
| 19 |
-
genai.configure(api_key=google_api_key)
|
| 20 |
-
model = genai.GenerativeModel(os.getenv("LLM_MODEL", "gemini-pro"))
|
| 21 |
-
LANGUAGE = "Odia"
|
| 22 |
-
|
| 23 |
-
# Models
|
| 24 |
-
class QuestionRequest(BaseModel):
|
| 25 |
-
question: str
|
| 26 |
-
|
| 27 |
-
class LLMResponseModel(BaseModel):
|
| 28 |
-
question_content: str
|
| 29 |
-
answer_language: str = LANGUAGE
|
| 30 |
-
reasoning_content: str
|
| 31 |
-
answer_content: str
|
| 32 |
-
|
| 33 |
-
def create_prompt(user_odia_question: str) -> str:
|
| 34 |
-
SIMPLE_PROMPT = PROMPTS["odia_reasoning_generation_prompt"]
|
| 35 |
-
prompt = SIMPLE_PROMPT.format(
|
| 36 |
-
user_odia_question=user_odia_question,
|
| 37 |
-
example_odia_question_json=example_odia_question_json,
|
| 38 |
-
example_answer_json=example_odia_answer_json
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
return prompt
|
| 42 |
-
# Functions
|
| 43 |
-
def chat_with_model(prompt: str) -> str:
|
| 44 |
-
try:
|
| 45 |
-
response = model.generate_content(prompt)
|
| 46 |
-
return response.text if response.text else "Error: Empty response"
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return f"Error: {str(e)}"
|
| 49 |
-
|
| 50 |
-
def clean_json_text(text: str) -> str:
|
| 51 |
-
if text.startswith("Error:"):
|
| 52 |
-
return text
|
| 53 |
-
|
| 54 |
-
# Remove markdown code blocks
|
| 55 |
-
text = text.strip()
|
| 56 |
-
if text.startswith("```"):
|
| 57 |
-
lines = text.split('\n')
|
| 58 |
-
if len(lines) > 2:
|
| 59 |
-
text = '\n'.join(lines[1:-1])
|
| 60 |
-
else:
|
| 61 |
-
text = text.strip("`").replace("json", "", 1).strip()
|
| 62 |
-
|
| 63 |
-
# Extract JSON content
|
| 64 |
-
first = text.find("{")
|
| 65 |
-
last = text.rfind("}")
|
| 66 |
-
if first != -1 and last != -1:
|
| 67 |
-
return text[first:last+1]
|
| 68 |
-
|
| 69 |
-
return text
|
| 70 |
-
|
| 71 |
-
def validate_output(raw_output: str, original_question: str):
|
| 72 |
-
cleaned = clean_json_text(raw_output)
|
| 73 |
-
|
| 74 |
-
if cleaned.startswith("Error:"):
|
| 75 |
-
return {
|
| 76 |
-
"question_content": original_question,
|
| 77 |
-
"answer_language": LANGUAGE,
|
| 78 |
-
"reasoning_content": f"Error occurred: {cleaned}",
|
| 79 |
-
"answer_content": "Unable to generate answer due to error",
|
| 80 |
-
"error": cleaned
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
# Try to parse and validate JSON
|
| 85 |
-
parsed_data = json.loads(cleaned)
|
| 86 |
-
validated = LLMResponseModel(**parsed_data)
|
| 87 |
-
return validated.model_dump()
|
| 88 |
-
except json.JSONDecodeError as je:
|
| 89 |
-
return {
|
| 90 |
-
"question_content": original_question,
|
| 91 |
-
"answer_language": LANGUAGE,
|
| 92 |
-
"reasoning_content": f"JSON parsing failed: {str(je)}",
|
| 93 |
-
"answer_content": "Unable to parse model response",
|
| 94 |
-
"error": f"JSON Error: {str(je)}"
|
| 95 |
-
}
|
| 96 |
-
except Exception as e:
|
| 97 |
-
return {
|
| 98 |
-
"question_content": original_question,
|
| 99 |
-
"answer_language": LANGUAGE,
|
| 100 |
-
"reasoning_content": f"Validation failed: {str(e)}",
|
| 101 |
-
"answer_content": "Unable to validate model response",
|
| 102 |
-
"error": f"Validation Error: {str(e)}"
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
def run_pipeline(question: str):
|
| 106 |
-
try:
|
| 107 |
-
# Use simple prompt if PROMPTS not available
|
| 108 |
-
prompt =create_prompt(user_odia_question=question)
|
| 109 |
-
raw_output = chat_with_model(prompt)
|
| 110 |
-
return validate_output(raw_output, question)
|
| 111 |
-
except Exception as e:
|
| 112 |
-
return {
|
| 113 |
-
"question_content": question,
|
| 114 |
-
"answer_language": LANGUAGE,
|
| 115 |
-
"reasoning_content": f"Pipeline error: {str(e)}",
|
| 116 |
-
"answer_content": "Unable to process question",
|
| 117 |
-
"error": f"Pipeline Error: {str(e)}"
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
# API
|
| 121 |
-
app = FastAPI(title="Odia Question Answering API", version="0.1.0")
|
| 122 |
-
|
| 123 |
-
@app.get("/")
|
| 124 |
-
async def root():
|
| 125 |
-
return {"message": "Odia Question Answering API is running", "status": "healthy"}
|
| 126 |
-
|
| 127 |
-
@app.get("/health")
|
| 128 |
-
async def health_check():
|
| 129 |
-
try:
|
| 130 |
-
# Test model connectivity
|
| 131 |
-
test_response = model.generate_content("Test")
|
| 132 |
-
return {
|
| 133 |
-
"status": "healthy",
|
| 134 |
-
"model": os.getenv("LLM_MODEL", "gemini-pro"),
|
| 135 |
-
"api_configured": bool(google_api_key)
|
| 136 |
-
}
|
| 137 |
-
except Exception as e:
|
| 138 |
-
return {
|
| 139 |
-
"status": "unhealthy",
|
| 140 |
-
"error": str(e),
|
| 141 |
-
"api_configured": bool(google_api_key)
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
@app.post("/generate")
|
| 145 |
-
async def generate_answer(request: QuestionRequest):
|
| 146 |
-
try:
|
| 147 |
-
if not request.question.strip():
|
| 148 |
-
raise HTTPException(status_code=400, detail="Question cannot be empty")
|
| 149 |
-
|
| 150 |
-
result = run_pipeline(request.question.strip())
|
| 151 |
-
|
| 152 |
-
# Check for critical errors that should return 500
|
| 153 |
-
if "error" in result and any(err_type in result["error"] for err_type in ["Error: ", "Pipeline Error:"]):
|
| 154 |
-
raise HTTPException(status_code=500, detail=f"Processing failed: {result['error']}")
|
| 155 |
-
|
| 156 |
-
return {"success": True, "data": result}
|
| 157 |
-
|
| 158 |
-
except HTTPException:
|
| 159 |
-
raise
|
| 160 |
-
except Exception as e:
|
| 161 |
-
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
| 162 |
-
|
| 163 |
if __name__ == "__main__":
|
| 164 |
print("Starting Odia Question Answering API...")
|
| 165 |
print(f"Google API Key configured: {'Yes' if google_api_key else 'No'}")
|
| 166 |
print(f"Model: {os.getenv('LLM_MODEL', 'gemini-pro')}")
|
| 167 |
host = os.getenv("ANSWER_SERVICE_HOST", "0.0.0.0")
|
| 168 |
port = int(os.getenv("ANSWER_SERVICE_PORT", "9000"))
|
| 169 |
-
uvicorn.run(app, host=host, port=port
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
import uvicorn
|
| 8 |
+
from prompts import PROMPTS
|
| 9 |
+
from llm_pipeline import example_odia_answer_json, example_odia_question_json
|
| 10 |
+
|
| 11 |
+
# Setup
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Check for required environment variables
|
| 15 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 16 |
+
if not google_api_key:
|
| 17 |
+
raise ValueError("GOOGLE_API_KEY not found in environment variables")
|
| 18 |
+
|
| 19 |
+
genai.configure(api_key=google_api_key)
|
| 20 |
+
model = genai.GenerativeModel(os.getenv("LLM_MODEL", "gemini-pro"))
|
| 21 |
+
LANGUAGE = "Odia"
|
| 22 |
+
|
| 23 |
+
# Models
|
| 24 |
+
class QuestionRequest(BaseModel):
|
| 25 |
+
question: str
|
| 26 |
+
|
| 27 |
+
class LLMResponseModel(BaseModel):
|
| 28 |
+
question_content: str
|
| 29 |
+
answer_language: str = LANGUAGE
|
| 30 |
+
reasoning_content: str
|
| 31 |
+
answer_content: str
|
| 32 |
+
|
| 33 |
+
def create_prompt(user_odia_question: str) -> str:
|
| 34 |
+
SIMPLE_PROMPT = PROMPTS["odia_reasoning_generation_prompt"]
|
| 35 |
+
prompt = SIMPLE_PROMPT.format(
|
| 36 |
+
user_odia_question=user_odia_question,
|
| 37 |
+
example_odia_question_json=example_odia_question_json,
|
| 38 |
+
example_answer_json=example_odia_answer_json
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return prompt
|
| 42 |
+
# Functions
|
| 43 |
+
def chat_with_model(prompt: str) -> str:
|
| 44 |
+
try:
|
| 45 |
+
response = model.generate_content(prompt)
|
| 46 |
+
return response.text if response.text else "Error: Empty response"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def clean_json_text(text: str) -> str:
|
| 51 |
+
if text.startswith("Error:"):
|
| 52 |
+
return text
|
| 53 |
+
|
| 54 |
+
# Remove markdown code blocks
|
| 55 |
+
text = text.strip()
|
| 56 |
+
if text.startswith("```"):
|
| 57 |
+
lines = text.split('\n')
|
| 58 |
+
if len(lines) > 2:
|
| 59 |
+
text = '\n'.join(lines[1:-1])
|
| 60 |
+
else:
|
| 61 |
+
text = text.strip("`").replace("json", "", 1).strip()
|
| 62 |
+
|
| 63 |
+
# Extract JSON content
|
| 64 |
+
first = text.find("{")
|
| 65 |
+
last = text.rfind("}")
|
| 66 |
+
if first != -1 and last != -1:
|
| 67 |
+
return text[first:last+1]
|
| 68 |
+
|
| 69 |
+
return text
|
| 70 |
+
|
| 71 |
+
def validate_output(raw_output: str, original_question: str):
|
| 72 |
+
cleaned = clean_json_text(raw_output)
|
| 73 |
+
|
| 74 |
+
if cleaned.startswith("Error:"):
|
| 75 |
+
return {
|
| 76 |
+
"question_content": original_question,
|
| 77 |
+
"answer_language": LANGUAGE,
|
| 78 |
+
"reasoning_content": f"Error occurred: {cleaned}",
|
| 79 |
+
"answer_content": "Unable to generate answer due to error",
|
| 80 |
+
"error": cleaned
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
# Try to parse and validate JSON
|
| 85 |
+
parsed_data = json.loads(cleaned)
|
| 86 |
+
validated = LLMResponseModel(**parsed_data)
|
| 87 |
+
return validated.model_dump()
|
| 88 |
+
except json.JSONDecodeError as je:
|
| 89 |
+
return {
|
| 90 |
+
"question_content": original_question,
|
| 91 |
+
"answer_language": LANGUAGE,
|
| 92 |
+
"reasoning_content": f"JSON parsing failed: {str(je)}",
|
| 93 |
+
"answer_content": "Unable to parse model response",
|
| 94 |
+
"error": f"JSON Error: {str(je)}"
|
| 95 |
+
}
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return {
|
| 98 |
+
"question_content": original_question,
|
| 99 |
+
"answer_language": LANGUAGE,
|
| 100 |
+
"reasoning_content": f"Validation failed: {str(e)}",
|
| 101 |
+
"answer_content": "Unable to validate model response",
|
| 102 |
+
"error": f"Validation Error: {str(e)}"
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
def run_pipeline(question: str):
|
| 106 |
+
try:
|
| 107 |
+
# Use simple prompt if PROMPTS not available
|
| 108 |
+
prompt =create_prompt(user_odia_question=question)
|
| 109 |
+
raw_output = chat_with_model(prompt)
|
| 110 |
+
return validate_output(raw_output, question)
|
| 111 |
+
except Exception as e:
|
| 112 |
+
return {
|
| 113 |
+
"question_content": question,
|
| 114 |
+
"answer_language": LANGUAGE,
|
| 115 |
+
"reasoning_content": f"Pipeline error: {str(e)}",
|
| 116 |
+
"answer_content": "Unable to process question",
|
| 117 |
+
"error": f"Pipeline Error: {str(e)}"
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
# API
|
| 121 |
+
app = FastAPI(title="Odia Question Answering API", version="0.1.0")
|
| 122 |
+
|
| 123 |
+
@app.get("/")
|
| 124 |
+
async def root():
|
| 125 |
+
return {"message": "Odia Question Answering API is running", "status": "healthy"}
|
| 126 |
+
|
| 127 |
+
@app.get("/health")
|
| 128 |
+
async def health_check():
|
| 129 |
+
try:
|
| 130 |
+
# Test model connectivity
|
| 131 |
+
test_response = model.generate_content("Test")
|
| 132 |
+
return {
|
| 133 |
+
"status": "healthy",
|
| 134 |
+
"model": os.getenv("LLM_MODEL", "gemini-pro"),
|
| 135 |
+
"api_configured": bool(google_api_key)
|
| 136 |
+
}
|
| 137 |
+
except Exception as e:
|
| 138 |
+
return {
|
| 139 |
+
"status": "unhealthy",
|
| 140 |
+
"error": str(e),
|
| 141 |
+
"api_configured": bool(google_api_key)
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
@app.post("/generate")
|
| 145 |
+
async def generate_answer(request: QuestionRequest):
|
| 146 |
+
try:
|
| 147 |
+
if not request.question.strip():
|
| 148 |
+
raise HTTPException(status_code=400, detail="Question cannot be empty")
|
| 149 |
+
|
| 150 |
+
result = run_pipeline(request.question.strip())
|
| 151 |
+
|
| 152 |
+
# Check for critical errors that should return 500
|
| 153 |
+
if "error" in result and any(err_type in result["error"] for err_type in ["Error: ", "Pipeline Error:"]):
|
| 154 |
+
raise HTTPException(status_code=500, detail=f"Processing failed: {result['error']}")
|
| 155 |
+
|
| 156 |
+
return {"success": True, "data": result}
|
| 157 |
+
|
| 158 |
+
except HTTPException:
|
| 159 |
+
raise
|
| 160 |
+
except Exception as e:
|
| 161 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
| 162 |
+
|
| 163 |
if __name__ == "__main__":
|
| 164 |
print("Starting Odia Question Answering API...")
|
| 165 |
print(f"Google API Key configured: {'Yes' if google_api_key else 'No'}")
|
| 166 |
print(f"Model: {os.getenv('LLM_MODEL', 'gemini-pro')}")
|
| 167 |
host = os.getenv("ANSWER_SERVICE_HOST", "0.0.0.0")
|
| 168 |
port = int(os.getenv("ANSWER_SERVICE_PORT", "9000"))
|
| 169 |
+
uvicorn.run(app, host=host, port=port)
|