import gradio as gr import pathlib import textwrap import google.generativeai as genai def to_markdown(text): """Converts text to Markdown format with proper indentation.""" text = text.replace('•', ' *') return textwrap.indent(text, '> ', lambda line: True) def chat(message, history): """Generates a response to the user's message using the Gemini API.""" genai.configure(api_key='AIzaSyCMBk81YmILNTok8hd6tYtJaevp1qbl6I0') # Replace with your actual API key model = genai.GenerativeModel('gemini-pro') try: response = model.generate_content(message, stream=True) for chunk in response: return to_markdown(chunk.text) # Format as Markdown except Exception as e: print(f"Error during generation: {e}") return "An error occurred while generating the response. Please try again later." chat_interface = gr.ChatInterface( fn=chat, title="Gemini Chat", description="Chat with an AI assistant powered by Gemini", theme="soft" ) chat_interface.launch()