AI4SDG / app.py
AidSdg's picture
Update app.py
2cce845 verified
Raw History Blame Contribute Delete
28.9 kB
import os
import gradio as gr
import google.generativeai as genai
from openai import OpenAI
import tempfile
from gtts import gTTS
import speech_recognition as sr
from PIL import Image
import docx
from PyPDF2 import PdfReader
from transformers import BlipProcessor, BlipForConditionalGeneration
import torch
from functools import lru_cache
import threading
import random
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
# -----------------------------
# OPTIONAL OCR SETUP
# -----------------------------
try:
import pytesseract
OCR_AVAILABLE = True
except ImportError:
OCR_AVAILABLE = False
print("⚠️ pytesseract not installed. OCR will be skipped.")
# -----------------------------
# API KEYS SETUP
# -----------------------------
# NOTE: These environmental variables must be set where the app is run.
# Using dummy values if not set to allow the app to initialize, but API calls will fail without real keys
GENAI_KEY = os.getenv("GEMINI_API_KEY", "dummy_genai_key")
OPENAI_KEY = os.getenv("OPENAI_API_KEY", "dummy_openai_key")
genai.configure(api_key=GENAI_KEY)
openai_client = OpenAI(api_key=OPENAI_KEY)
# -----------------------------
# CHATCDS SYSTEM PROMPT
# -----------------------------
SYSTEM_PROMPT = """
You are ChatCDS, an AI assistant created by the Advanced Initiative for Deploying Sustainable Development Goals (AIDSDG),
powered by AI4SDG (Artificial Intelligence for Scaling Sustainable Development Goals). You help NYSC corps members plan, align, and scale their Community Development Service (CDS) projects
with the United Nations Sustainable Development Goals (SDGs).
You are knowledgeable about SDG targets, community impact design, budgeting, and proposal writing.
Always speak with encouragement and clarity. When relevant, show which SDG(s) align with the idea.
Be professional, concise, and human-sounding.
"""
# -----------------------------
# WELCOME MESSAGES
# -----------------------------
WELCOME_MESSAGES = [
"🌟 Welcome to ChatCDS! Ready to make a real impact in your community? Let's align your CDS project with the SDGs and create lasting change!",
"πŸ‘‹ Hello, changemaker! ChatCDS is here to help you design, plan, and scale your Community Development Service project. What SDG goal inspires you today?",
"πŸš€ Great to see you! Let's turn your CDS ideas into actionable projects that transform communities. How can I assist you today?",
"πŸ’‘ Welcome back! Your community needs your innovative ideas. Let's work together to align your CDS project with sustainable development goals!",
"🌍 Hello! Ready to be a catalyst for change? ChatCDS is your partner in guiding your CDS journey from concept to implementation. Let's get started!",
"✨ Welcome, corps member! Every great project starts with a single idea. Share yours, and let's make it SDG-aligned and impactful!",
"🎯 Hi there! ChatCDS is your partner in creating meaningful community impact. Let's explore how your CDS project can contribute to the SDGs!",
"🌱 Welcome! Your service year is an opportunity to plant seeds of change. Let's nurture your CDS ideas into sustainable community solutions!",
]
MOTIVATIONAL_QUOTES = [
"πŸ’ͺ Remember: Small actions, when multiplied by millions, can transform the world!",
"🌟 Your CDS project has the power to create ripples of positive change in your community!",
"πŸš€ Every SDG goal achieved starts with someone like you taking action!",
"πŸ’‘ Innovation in community service doesn't require perfectionβ€”it requires passion and commitment!",
"🌍 You're not just servingβ€”you're building a legacy of sustainable development!",
"✨ The best time to start making a difference was yesterday. The second best time is now!",
"🎯 Your unique perspective as a corps member is exactly what your community needs!",
]
# -----------------------------
# MEMORY MANAGEMENT & CHAT HISTORY
# -----------------------------
chat_sessions = {}
current_session_id = None
api_user_sessions = {}
def get_welcome_message():
welcome = random.choice(WELCOME_MESSAGES)
motivation = random.choice(MOTIVATIONAL_QUOTES)
return f"{welcome}\n\n{motivation}"
def generate_session_id():
import time
return f"session_{int(time.time())}_{random.randint(1000, 9999)}"
def save_current_session(history: List[dict]):
"""Saves the current chat history (in dictionary format) to chat_sessions."""
global current_session_id, chat_sessions
if current_session_id and len(history) > 1:
from datetime import datetime
first_user_msg = "New Chat"
for msg in history:
# History is list of dicts
content = msg.get("content")
role = msg.get("role")
if role == "user" and content:
clean_content = content.replace('\n', ' ').strip()
first_user_msg = clean_content[:60] + ("..." if len(clean_content) > 60 else "")
break
chat_sessions[current_session_id] = {
"history": history.copy(),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"preview": first_user_msg,
"title": first_user_msg
}
def get_chat_history_list():
if not chat_sessions:
return []
chat_list = []
for session_id, session_data in sorted(chat_sessions.items(), key=lambda x: x[1]["timestamp"], reverse=True):
clean_title = session_data['title'].replace('\n', ' ').strip()
display_text = f"πŸ•’ {session_data['timestamp']} | {clean_title}"
chat_list.append(display_text)
return chat_list
def load_chat_session(selected_chat):
"""Loads history in the required dictionary format for Gradio."""
global current_session_id
if not selected_chat or selected_chat == "" or not chat_sessions:
new_history = initialize_chat()
new_choices = get_chat_history_list()
# Returns history (list of dicts), blank input, and updated dropdown
return new_history, "", gr.Dropdown(choices=new_choices, value=None)
for session_id, session_data in chat_sessions.items():
clean_title = session_data['title'].replace('\n', ' ').strip()
display_text = f"πŸ•’ {session_data['timestamp']} | {clean_title}"
if display_text == selected_chat or display_text.strip() == selected_chat.strip():
current_session_id = session_id
new_choices = get_chat_history_list()
# History is already in dictionary format (list of dicts)
return session_data["history"], "", gr.Dropdown(choices=new_choices, value=selected_chat)
new_history = initialize_chat()
new_choices = get_chat_history_list()
return new_history, "", gr.Dropdown(choices=new_choices, value=None)
def delete_chat_session(selected_chat):
global chat_sessions
if not selected_chat or not chat_sessions:
new_choices = get_chat_history_list()
return gr.Dropdown(choices=new_choices, value=None), "No chat selected to delete"
for session_id, session_data in list(chat_sessions.items()):
clean_title = session_data['title'].replace('\n', ' ').strip()
display_text = f"πŸ•’ {session_data['timestamp']} | {clean_title}"
if display_text == selected_chat or display_text.strip() == selected_chat.strip():
del chat_sessions[session_id]
new_choices = get_chat_history_list()
return gr.Dropdown(choices=new_choices, value=None), f"βœ… Chat deleted successfully!"
new_choices = get_chat_history_list()
return gr.Dropdown(choices=new_choices, value=None), "❌ Chat not found"
def rename_chat_session(selected_chat, new_title):
global chat_sessions
if not selected_chat or not chat_sessions or not new_title.strip():
new_choices = get_chat_history_list()
return gr.Dropdown(choices=new_choices, value=selected_chat), "Please provide a valid title"
for session_id, session_data in chat_sessions.items():
clean_title = session_data['title'].replace('\n', ' ').strip()
display_text = f"πŸ•’ {session_data['timestamp']} | {clean_title}"
if display_text == selected_chat or display_text.strip() == selected_chat.strip():
chat_sessions[session_id]["title"] = new_title.strip()
new_choices = get_chat_history_list()
new_display = f"πŸ•’ {session_data['timestamp']} | {new_title.strip()}"
return gr.Dropdown(choices=new_choices, value=new_display), f"βœ… Chat renamed to: {new_title}"
new_choices = get_chat_history_list()
return gr.Dropdown(choices=new_choices, value=selected_chat), "❌ Chat not found"
def start_new_chat(history):
global current_session_id
save_current_session(history) # history is list of dicts
current_session_id = generate_session_id()
new_history = initialize_chat()
new_choices = get_chat_history_list()
return new_history, "", None, None, None, gr.Dropdown(choices=new_choices, value=None), ""
def generate_ai_response_gradio(messages: List[dict]):
"""Handles the model call for the Gradio interface."""
prompt = SYSTEM_PROMPT + "\n\nConversation so far:\n"
# Use only the last 6 messages (system instructions + 5 turns)
for m in messages[-6:]:
prompt += f"{m['role'].capitalize()}: {m['content']}\n"
try:
# Gemini call
gemini_model = genai.GenerativeModel("gemini-1.5-flash")
response = gemini_model.generate_content(prompt)
reply = response.text.strip()
except Exception as e:
try:
# OpenAI fallback using structured message format
ai_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for m in messages[-6:]:
ai_messages.append({"role": m['role'], "content": m['content']})
completion = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=ai_messages
)
reply = completion.choices[0].message.content.strip()
except Exception as e:
reply = f"⚠️ Error generating response: {e}"
return reply
def chat_with_ai_api(user_input: str, user_id: str, history: List[dict] = None):
"""Handles the model call for the external FastAPI API."""
if user_id not in api_user_sessions:
api_user_sessions[user_id] = []
if history is None:
history = api_user_sessions[user_id]
# 1. Prepare messages for model
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
messages.extend(history[-10:])
messages.append({"role": "user", "content": user_input})
prompt = SYSTEM_PROMPT + "\n\nConversation history:\n"
for msg in messages[-10:]:
role = msg.get("role", "user")
content = msg.get("content", "")
prompt += f"{role.capitalize()}: {content}\n"
# 2. Call model
try:
gemini_model = genai.GenerativeModel("gemini-1.5-flash")
response = gemini_model.generate_content(prompt)
reply = response.text.strip()
except Exception:
try:
completion = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
reply = completion.choices[0].message.content.strip()
except Exception:
reply = f"⚠️ Sorry, I'm having trouble processing your request. Please try again."
# 3. Update internal API session memory
api_user_sessions[user_id].append({"role": "user", "content": user_input})
api_user_sessions[user_id].append({"role": "assistant", "content": reply})
if len(api_user_sessions[user_id]) > 50:
api_user_sessions[user_id] = api_user_sessions[user_id][-50:]
return reply
def transcribe_audio(audio_file):
recognizer = sr.Recognizer()
try:
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
return recognizer.recognize_google(audio)
except Exception as e:
print(f"Audio transcription error: {e}")
return "Sorry, I couldn't process the audio input."
def generate_voice_output(text):
if not text:
return None
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmpfile:
tts = gTTS(text=text, lang="en")
tts.save(tmpfile.name)
return tmpfile.name
except Exception as e:
print(f"TTS generation error: {e}")
return None
def extract_text_from_pdf(file_path):
try:
reader = PdfReader(file_path)
text = ""
for page in reader.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
return text
except Exception:
return "[Could not read PDF content]"
def extract_text_from_docx(file_path):
try:
doc = docx.Document(file_path)
text = "\n".join([para.text for para in doc.paragraphs])
return text
except Exception:
return "[Could not read DOCX content]"
def extract_text_from_txt(file_path):
try:
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
except Exception:
return "[Could not read TXT content]"
@lru_cache(maxsize=32)
def summarize_text(text):
if len(text.strip()) == 0:
return ""
# Simple chunking for very long documents
chunk_size = 2000
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
summaries = []
for chunk in chunks:
prompt = f"Please summarize the following content in 3–5 concise sentences for ChatCDS:\n\n{chunk}"
try:
summary_response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "You are a helpful summarizer."},
{"role": "user", "content": prompt}]
)
summary = summary_response.choices[0].message.content.strip()
summaries.append(summary)
except Exception:
summaries.append("[Could not summarize chunk]")
return " ".join(summaries)
processor = None
blip_model = None
image_caption_cache = {}
def load_blip_models():
global processor, blip_model
if processor is None:
try:
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
except Exception as e:
print(f"Failed to load BLIP models: {e}")
def describe_image_advanced(image_path):
load_blip_models()
if processor is None or blip_model is None:
return "[Image analysis skipped: Model loading failed]"
if image_path in image_caption_cache:
return image_caption_cache[image_path]
try:
img = Image.open(image_path).convert('RGB')
# Generates caption focusing on community-relevant details
inputs = processor(images=img, text="a detailed description of the image relevant to community projects", return_tensors="pt")
out = blip_model.generate(**inputs, max_length=50)
caption = processor.decode(out[0], skip_special_tokens=True)
ocr_text_placeholder = ""
if OCR_AVAILABLE:
import pytesseract
ocr_text = pytesseract.image_to_string(img).strip()
if ocr_text:
ocr_text_placeholder = f" (Contains text: {ocr_text[:100]}...)"
result = f"[Image description: {caption}.{ocr_text_placeholder}]"
image_caption_cache[image_path] = result
return result
except Exception as e:
print(f"Image analysis error: {e}")
return "[Could not analyze image]"
def async_file_image_processing(file_input, image_input, user_input_container):
"""Processes files and images and appends context to the user input container."""
if file_input is not None:
file_path = file_input.name
ext = os.path.splitext(file_path)[1].lower()
file_text = ""
if ext == ".pdf":
file_text = extract_text_from_pdf(file_path)
elif ext == ".docx":
file_text = extract_text_from_docx(file_path)
elif ext == ".txt":
file_text = extract_text_from_txt(file_path)
else:
user_input_container.append(f"\n[Uploaded file: {os.path.basename(file_path)} - Type not supported for summary]")
if file_text:
summaries = summarize_text(file_text)
if summaries:
user_input_container.append(f"\n[File summary: {summaries}]")
if image_input is not None:
img_desc = describe_image_advanced(image_input)
user_input_container.append(f"\n{img_desc}")
def process_input(user_input, audio_input, file_input, image_input, history):
global current_session_id
try:
if current_session_id is None:
current_session_id = generate_session_id()
# 1. Handle Audio Input
if audio_input is not None:
transcribed_text = transcribe_audio(audio_input)
user_input = transcribed_text if user_input.strip() == "" else f"{user_input}\n({transcribed_text})"
# 2. Prepare combined input
user_input_container = [user_input]
# 3. Process files and images
thread = threading.Thread(target=async_file_image_processing, args=(file_input, image_input, user_input_container))
thread.start()
thread.join()
# The first element is the original text input
main_user_input = user_input_container[0]
# Subsequent elements are context from attachments
context_input = "".join(user_input_container[1:])
# Combine text and context for the full prompt sent to the model
full_prompt = main_user_input
if context_input:
full_prompt = f"{main_user_input}\n\n[CONTEXT FROM ATTACHMENTS]:{context_input}"
# 4. Generate AI response
# Create temporary message list for the model, including the new user message
new_user_message = {"role": "user", "content": full_prompt}
model_history = history + [new_user_message]
reply = generate_ai_response_gradio(model_history)
# 5. Update Gradio history (Crucial fix: using dictionary format)
# The Gradio component only shows 'main_user_input' to the user, not the hidden context string
history.append({"role": "user", "content": main_user_input})
history.append({"role": "assistant", "content": reply})
# 6. Generate voice output
voice_file = generate_voice_output(reply)
# 7. Save session and update dropdown
save_current_session(history)
new_choices = get_chat_history_list()
# 8. Return outputs, clearing inputs after successful submission
# Returning None for file/audio/image components effectively clears them
return history, voice_file, "", None, None, None, gr.Dropdown(choices=new_choices, value=None)
except Exception as e:
print(f"Critical runtime error in process_input: {e}")
# If any part of the function fails, return a safe state with an error message
# And ensure inputs are cleared to allow re-submission
history.append({"role": "assistant", "content": f"⚠️ An error occurred: {e}. Please try again or simplify your request."})
new_choices = get_chat_history_list()
return history, None, "", None, None, None, gr.Dropdown(choices=new_choices, value=None)
def initialize_chat():
global current_session_id
current_session_id = generate_session_id()
welcome_msg = get_welcome_message()
# FIXED: Use dictionary format for modern Gradio Chatbot
initial_history = [{"role": "assistant", "content": welcome_msg}]
return initial_history
# --- FastAPI Setup ---
app = FastAPI(title="ChatCDS API", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Message(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
message: str
user_id: str
history: Optional[List[Message]] = []
class ChatResponse(BaseModel):
response: str
user_id: str
timestamp: str
# Fixed FastAPI route to avoid conflict with Gradio root mount
@app.get("/api-status")
async def api_root():
"""Provides status and endpoint information for the FastAPI service."""
return {
"status": "online",
"service": "ChatCDS API",
"version": "1.0.0",
"endpoints": {
"status_check": "/api-status",
"health_check": "/health",
"chat": "/api/chat",
"docs": "/docs"
},
"note": "Gradio UI is mounted at the root path /"
}
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"active_sessions": len(api_user_sessions),
"models": "Gemini + OpenAI"
}
@app.post("/api/chat", response_model=ChatResponse)
async def chat_endpoint(request: ChatRequest):
try:
# Convert Pydantic list of Message models back into a list of dictionaries for the chat_with_ai_api function
history = [msg.dict() for msg in request.history] if request.history else []
response = chat_with_ai_api(
user_input=request.message,
user_id=request.user_id,
history=history
)
from datetime import datetime
return ChatResponse(
response=response,
user_id=request.user_id,
timestamp=datetime.now().isoformat()
)
except Exception as e:
print(f"API Error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/clear-session")
async def clear_session(user_id: str):
if user_id in api_user_sessions:
del api_user_sessions[user_id]
return {"status": "success", "message": f"Session cleared for {user_id}"}
return {"status": "info", "message": "No session found"}
# --- Gradio UI Setup ---
custom_css = """
#chatbot {height: 460px; overflow-y: auto; background-color: #f7f9fc;}
textarea {resize: none !important;}
.gr-text-input textarea {line-height: 1.5em; font-size: 15px;}
.input-accordion {margin-top: 10px; border: 1px solid #e0e0e0; border-radius: 8px; padding: 10px;}
.chat-history-section {background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%); padding: 15px; border-radius: 8px; margin-bottom: 15px; border: 1px solid #4a6fa5;}
.chat-history-section label {color: #ffffff !important; font-weight: 500;}
.chat-history-section .markdown {color: #ffffff !important;}
.new-chat-btn {margin-top: 10px;}
.status-message {padding: 8px; border-radius: 4px; margin-top: 8px; text-align: center; font-size: 14px;}
.gradio-container {font-family: 'Inter', sans-serif;}
"""
def create_ui():
initial_history = initialize_chat()
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("""
## 🌍 ChatCDS β€” Artificial Intelligence for Community Development Service
Empowering NYSC corps members to plan, align, and scale their CDS projects with the UN SDGs.
**πŸ”Œ API Endpoint:** Status at `/api-status`, Chat at `/api/chat`.
""")
with gr.Accordion("πŸ“‚ Chat History", open=False, elem_classes="chat-history-section"):
gr.Markdown("### πŸ’¬ Access your previous conversations", elem_classes="markdown")
chat_history_dropdown = gr.Dropdown(
label="πŸ“‹ Your Chat Sessions",
choices=[],
interactive=True,
allow_custom_value=False,
value=None
)
with gr.Row():
load_chat_btn = gr.Button("πŸ“₯ Load Chat", scale=1, variant="primary", size="sm")
new_chat_btn = gr.Button("βž• New Chat", scale=1, variant="secondary", size="sm")
with gr.Row():
with gr.Column(scale=2):
rename_input = gr.Textbox(
label="✏️ Rename Selected Chat",
placeholder="Enter new title...",
lines=1
)
rename_btn = gr.Button("πŸ’Ύ Rename", size="sm", variant="secondary")
with gr.Column(scale=1):
gr.Markdown("### ")
delete_btn = gr.Button("πŸ—‘οΈ Delete Chat", size="sm", variant="stop")
status_message = gr.Textbox(
label="",
interactive=False,
show_label=False,
elem_classes="status-message"
)
chatbot = gr.Chatbot(
elem_id="chatbot",
label="ChatCDS",
value=initial_history,
show_label=False,
type="messages"
)
with gr.Row():
txt_input = gr.Textbox(
label="Type your message (Press Shift + Enter to send)",
placeholder="Ask about your CDS idea, SDG, or project plan...",
lines=3,
scale=9
)
submit_btn = gr.Button(
"πŸš€ Send",
variant="primary",
scale=1,
min_width=80
)
# FIX: Explicitly wrapped inputs in gr.Column to fix "littered" layout
with gr.Accordion("βž• Additional Options (Voice, Files, Images)", open=False, elem_classes="input-accordion"):
with gr.Column():
voice_input = gr.Audio(
label="🎀 Voice Input",
type="filepath",
# Removed redundant elem_id
)
file_input = gr.File(
label="πŸ“„ Upload Document (PDF, DOCX, TXT)",
file_types=[".pdf", ".docx", ".txt"],
# Removed redundant elem_id
)
image_input = gr.Image(
label="πŸ–Ό Upload Image",
type="filepath",
# Removed redundant elem_id
)
voice_output = gr.Audio(label="πŸ”Š Voice Output", type="filepath")
# -----------------------------
# EVENT HANDLERS
# -----------------------------
demo.load(
lambda: (initialize_chat(), gr.Dropdown(choices=get_chat_history_list(), value=None)),
outputs=[chatbot, chat_history_dropdown]
)
# Send button click and Enter key submit are identical
submit_args = {
"fn": process_input,
"inputs": [txt_input, voice_input, file_input, image_input, chatbot],
"outputs": [chatbot, voice_output, txt_input, voice_input, file_input, image_input, chat_history_dropdown]
}
submit_btn.click(**submit_args)
txt_input.submit(**submit_args)
load_chat_btn.click(
load_chat_session,
inputs=[chat_history_dropdown],
outputs=[chatbot, txt_input, chat_history_dropdown]
)
new_chat_btn.click(
start_new_chat,
inputs=[chatbot],
outputs=[chatbot, txt_input, voice_input, file_input, image_input, chat_history_dropdown, status_message]
)
rename_btn.click(
rename_chat_session,
inputs=[chat_history_dropdown, rename_input],
outputs=[chat_history_dropdown, status_message]
)
delete_btn.click(
delete_chat_session,
inputs=[chat_history_dropdown],
outputs=[chat_history_dropdown, status_message]
)
return demo
# -----------------------------
# APPLICATION ENTRY POINT
# -----------------------------
# Mount the Gradio interface onto the FastAPI app instance
demo = create_ui()
app = gr.mount_gradio_app(app, demo, path="/")
# This block ensures the combined FastAPI/Gradio application is run
# correctly when the script is executed directly.
if __name__ == "__main__":
print("Starting ChatCDS application...")
uvicorn.run(app, host="0.0.0.0", port=7860)