Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import pathlib
|
| 4 |
+
import textwrap
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
except ImportError:
|
| 9 |
+
print("WARNING: google.generativeai not found. Install with `pip install google-generativeai` for AI-powered responses.")
|
| 10 |
+
genai = None
|
| 11 |
+
|
| 12 |
+
from IPython.display import display # Only for development/testing
|
| 13 |
+
from IPython.display import Markdown # Only for development/testing
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def to_markdown(text):
|
| 17 |
+
"""Converts text to Markdown format with proper indentation.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
text (str): The text to convert.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
str: The converted Markdown text.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
text = text.replace('•', ' *')
|
| 27 |
+
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def chat(chat_history):
|
| 31 |
+
"""Generates a response based on the chat history.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
chat_history (list): A list containing user messages and AI responses.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
str: The AI's response to the latest user message.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
if not genai:
|
| 41 |
+
return "AI responses are currently unavailable. Please install `google-generativeai` for this functionality."
|
| 42 |
+
|
| 43 |
+
user_message = chat_history[-1] # Get the latest user message
|
| 44 |
+
try:
|
| 45 |
+
response = model.generate_content(user_message, stream=True)
|
| 46 |
+
for chunk in response:
|
| 47 |
+
return chunk.text # Return the first generated text chunk
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error during generation: {e}")
|
| 50 |
+
return "An error occurred while generating the response. Please try again later."
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
interface = gr.Interface(
|
| 54 |
+
fn=chat,
|
| 55 |
+
inputs="chat",
|
| 56 |
+
outputs="textbox",
|
| 57 |
+
title="Gradio Chat App",
|
| 58 |
+
description="Chat with an AI assistant (requires `google-generativeai`)",
|
| 59 |
+
catch_exceptions=True, # Catch exceptions and display informative messages
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
interface.launch()
|
| 63 |
+
|
| 64 |
+
# Code below is for development/testing purposes only (not required for Gradio app)
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
if not genai:
|
| 67 |
+
print("WARNING: google.generativeai not found. Install with `pip install google-generativeai` for AI-powered responses.")
|
| 68 |
+
|
| 69 |
+
genai.configure(api_key='AIzaSyCMBk81YmILNTok8hd6tYtJaevp1qbl6I0') # Replace with your actual API key
|
| 70 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 71 |
+
|
| 72 |
+
chat_history = []
|
| 73 |
+
while True:
|
| 74 |
+
user_message = input("You: ")
|
| 75 |
+
chat_history.append(user_message)
|
| 76 |
+
response = chat(chat_history)
|
| 77 |
+
print(f"AI: {response}")
|
| 78 |
+
chat_history.append(response)
|
| 79 |
+
print("-" * 80)
|