import gradio as gr import pathlib import textwrap try: import google.generativeai as genai except ImportError: print("WARNING: google.generativeai not found. Install with `pip install google-generativeai` for AI-powered responses.") genai = None from IPython.display import display # Only for development/testing from IPython.display import Markdown # Only for development/testing def to_markdown(text): """Converts text to Markdown format with proper indentation. Args: text (str): The text to convert. Returns: str: The converted Markdown text. """ text = text.replace('•', ' *') return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True)) def chat(user_message): """Generates a response to the user's message. Args: user_message (str): The user's message. Returns: str: The AI-generated response (or a message indicating unavailability). """ genai.configure(api_key='AIzaSyCMBk81YmILNTok8hd6tYtJaevp1qbl6I0') # Replace with your actual API key model = genai.GenerativeModel('gemini-pro') if not genai: return "AI responses are currently unavailable. Please install `google-generativeai` for this functionality." try: response = model.generate_content(user_message, stream=True) for chunk in response: return chunk.text # Return the first generated text chunk except Exception as e: print(f"Error during generation: {e}") return "An error occurred while generating the response. Please try again later." interface = gr.Interface( fn=chat, inputs="textbox", outputs="textbox", title="Gradio Chat App", description="Chat with an AI assistant (requires `google-generativeai`)", ) interface.launch()