Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mport gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from zoho_client_mcp import mcp # Import the MCP instance from our tools file
|
| 5 |
+
|
| 6 |
+
# --- 1. Gemini Client Initialization ---
|
| 7 |
+
# We configure the Gemini client directly in the app before launching the MCP.
|
| 8 |
+
# This assumes os.environ['GOOGLE_API_KEY'] is set in config.py.
|
| 9 |
+
def configure_gemini():
|
| 10 |
+
"""Initializes the Gemini client using the API key from environment variables."""
|
| 11 |
+
try:
|
| 12 |
+
genai.configure(api_key=os.environ.get('GOOGLE_API_KEY'))
|
| 13 |
+
print("Gemini API configured successfully for conversational use.")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error configuring Gemini API: {e}")
|
| 16 |
+
|
| 17 |
+
# --- 2. Gradio Chat Interface Definition ---
|
| 18 |
+
# This defines the web UI required for conversation and file uploads [3, 13, 16, 17].
|
| 19 |
+
def chat_interface():
|
| 20 |
+
# Gradio's gr.ChatInterface is used to build a standard chat UI.
|
| 21 |
+
# The submission logic is handled automatically by FastMCP when mcp_server=True is used.
|
| 22 |
+
|
| 23 |
+
# The file input is added directly to allow attachments (images/PDFs) [13, 18].
|
| 24 |
+
# Files uploaded here will be available to the 'process_document' tool [18].
|
| 25 |
+
file_component = gr.File(label="Upload Image or PDF Document", type="filepath")
|
| 26 |
+
|
| 27 |
+
# The gr.ChatInterface sets up the UI structure for the conversation.
|
| 28 |
+
iface = gr.ChatInterface(
|
| 29 |
+
# The function passed here is typically the conversational engine.
|
| 30 |
+
# When mcp_server=True, the MCP framework handles the routing.
|
| 31 |
+
fn=lambda msg, history: f"Thinking...",
|
| 32 |
+
chatbot=gr.Chatbot(height=400),
|
| 33 |
+
textbox=gr.Textbox(placeholder="Ask me to create a contact, search records, or upload a document...", scale=7),
|
| 34 |
+
theme="soft",
|
| 35 |
+
title="Zoho CRM Agent (Gemini + FastMCP)",
|
| 36 |
+
submit_btn="Send Command",
|
| 37 |
+
clear_btn="Clear History",
|
| 38 |
+
# We attach the file component to the interface so users can upload [3, 16].
|
| 39 |
+
additional_inputs=[file_component]
|
| 40 |
+
)
|
| 41 |
+
return iface
|
| 42 |
+
|
| 43 |
+
# --- 3. Server Launch ---
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
# Ensure Gemini is ready [11, 19].
|
| 47 |
+
configure_gemini()
|
| 48 |
+
|
| 49 |
+
# Get the defined Gradio UI
|
| 50 |
+
demo = chat_interface()
|
| 51 |
+
|
| 52 |
+
# Launch the Gradio UI, instructing it to also start the FastMCP server [4].
|
| 53 |
+
# Gradio automatically connects the UI to the running 'mcp' instance when mcp_server=True [3].
|
| 54 |
+
print("Starting Gradio Web UI and FastMCP Server...")
|
| 55 |
+
demo.launch(
|
| 56 |
+
server_name="0.0.0.0",
|
| 57 |
+
server_port=7860,
|
| 58 |
+
mcp_server=True, # Crucial: Starts the FastMCP server instance 'mcp' [4].
|
| 59 |
+
mcp=mcp # Passes the specific FastMCP instance we defined [4].
|
| 60 |
+
)
|