Spaces:
Running
Running
yucelgumus61 commited on
Commit ·
fe980c5
1
Parent(s): 24503f7
Hugging Face için proje hazırlığı
Browse files- .gitignore +28 -0
- README.md +45 -2
- app.py +34 -54
- cli_app.py +57 -0
- pydub_patch.py +9 -0
- requirements.txt +3 -3
- run.py +17 -0
.gitignore
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Environment variables
|
| 2 |
+
.env
|
| 3 |
+
|
| 4 |
+
# Virtual environment
|
| 5 |
+
venv/
|
| 6 |
+
env/
|
| 7 |
+
ENV/
|
| 8 |
+
|
| 9 |
+
# Python cache files
|
| 10 |
+
__pycache__/
|
| 11 |
+
*.py[cod]
|
| 12 |
+
*$py.class
|
| 13 |
+
.pytest_cache/
|
| 14 |
+
|
| 15 |
+
# Distribution / packaging
|
| 16 |
+
dist/
|
| 17 |
+
build/
|
| 18 |
+
*.egg-info/
|
| 19 |
+
|
| 20 |
+
# IDE files
|
| 21 |
+
.idea/
|
| 22 |
+
.vscode/
|
| 23 |
+
*.swp
|
| 24 |
+
*.swo
|
| 25 |
+
|
| 26 |
+
# OS specific files
|
| 27 |
+
.DS_Store
|
| 28 |
+
Thumbs.db
|
README.md
CHANGED
|
@@ -4,8 +4,8 @@ emoji: 🤖
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
|
@@ -13,3 +13,46 @@ license: mit
|
|
| 13 |
# ChatBot Pro
|
| 14 |
|
| 15 |
A simple but powerful chatbot application built with Google Gemini AI model and Gradio interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.26.0
|
| 8 |
+
app_file: run.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
|
|
|
| 13 |
# ChatBot Pro
|
| 14 |
|
| 15 |
A simple but powerful chatbot application built with Google Gemini AI model and Gradio interface.
|
| 16 |
+
|
| 17 |
+
## Setup and Installation
|
| 18 |
+
|
| 19 |
+
### Local Development
|
| 20 |
+
1. Clone this repository
|
| 21 |
+
2. Create a virtual environment:
|
| 22 |
+
```
|
| 23 |
+
python -m venv venv
|
| 24 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 25 |
+
```
|
| 26 |
+
3. Install dependencies:
|
| 27 |
+
```
|
| 28 |
+
pip install -r requirements.txt
|
| 29 |
+
```
|
| 30 |
+
4. Make sure you have a `.env` file with your Google API key:
|
| 31 |
+
```
|
| 32 |
+
GOOGLE_API_KEY=your_api_key_here
|
| 33 |
+
```
|
| 34 |
+
5. Run the application:
|
| 35 |
+
```
|
| 36 |
+
python run.py
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
### Deploying to Hugging Face Spaces
|
| 40 |
+
1. Create a new Space on Hugging Face and select Gradio as the SDK
|
| 41 |
+
2. Use this repository as the source (you can link your GitHub repository or upload files directly)
|
| 42 |
+
3. Add your Google API key as a secret named `HF_GOOGLE_API_KEY` in the Space settings
|
| 43 |
+
4. Deploy the Space and it should run automatically
|
| 44 |
+
|
| 45 |
+
## Features
|
| 46 |
+
|
| 47 |
+
- Interactive chat interface
|
| 48 |
+
- Persistent chat sessions
|
| 49 |
+
- Gemini AI powered responses
|
| 50 |
+
- Clean and modern UI
|
| 51 |
+
|
| 52 |
+
## Troubleshooting
|
| 53 |
+
|
| 54 |
+
If you encounter any issues:
|
| 55 |
+
|
| 56 |
+
1. Verify your API key is correct in the `.env` file or Hugging Face Space secrets
|
| 57 |
+
2. Make sure all dependencies are installed correctly
|
| 58 |
+
3. Check the console for any error messages
|
app.py
CHANGED
|
@@ -6,12 +6,16 @@ from dotenv import load_dotenv
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
# Configure Google API
|
| 10 |
-
api_key = os.getenv("GOOGLE_API_KEY")
|
| 11 |
if not api_key:
|
| 12 |
-
raise ValueError("
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Global chat sessions dictionary to maintain sessions between requests
|
| 17 |
chat_sessions = {}
|
|
@@ -20,66 +24,42 @@ def get_chat_session(session_id):
|
|
| 20 |
"""Get an existing chat session or create a new one."""
|
| 21 |
if session_id not in chat_sessions:
|
| 22 |
try:
|
| 23 |
-
model = genai.GenerativeModel('gemini-
|
| 24 |
chat_sessions[session_id] = model.start_chat(history=[])
|
| 25 |
except Exception as e:
|
| 26 |
raise Exception(f"Failed to create chat session: {str(e)}")
|
| 27 |
|
| 28 |
return chat_sessions[session_id]
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
|
|
|
|
|
|
| 33 |
chat = get_chat_session(session_id)
|
| 34 |
-
response = chat.send_message(
|
| 35 |
return response.text
|
| 36 |
except Exception as e:
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def clear_chat(session_id="default"):
|
| 49 |
-
"""Clear the chat history and reset the session."""
|
| 50 |
-
if session_id in chat_sessions:
|
| 51 |
-
del chat_sessions[session_id]
|
| 52 |
-
return None
|
| 53 |
-
|
| 54 |
-
# Build Gradio interface
|
| 55 |
-
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 56 |
-
gr.Markdown("# ChatBot Pro - Gemini AI")
|
| 57 |
-
|
| 58 |
-
with gr.Row():
|
| 59 |
-
with gr.Column():
|
| 60 |
-
chatbot = gr.Chatbot(
|
| 61 |
-
label="Sohbet Geçmişi",
|
| 62 |
-
height=600,
|
| 63 |
-
show_copy_button=True
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
with gr.Row():
|
| 67 |
-
msg = gr.Textbox(
|
| 68 |
-
label="Mesajınız",
|
| 69 |
-
placeholder="Sorunuzu buraya yazın...",
|
| 70 |
-
scale=8,
|
| 71 |
-
container=False
|
| 72 |
-
)
|
| 73 |
-
send = gr.Button("Gönder", scale=1)
|
| 74 |
-
clear = gr.Button("Temizle", scale=1)
|
| 75 |
-
|
| 76 |
-
gr.Markdown("### Gemini AI tabanlı bir sohbet uygulaması")
|
| 77 |
-
|
| 78 |
-
# Set up event handlers
|
| 79 |
-
msg.submit(chat_with_gemini, [msg, chatbot], [msg, chatbot])
|
| 80 |
-
send.click(chat_with_gemini, [msg, chatbot], [msg, chatbot])
|
| 81 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 82 |
-
clear.click(clear_chat, None, None, queue=False)
|
| 83 |
|
|
|
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Configure Google API - First check for Hugging Face Spaces secret, then fallback to .env
|
| 10 |
+
api_key = os.getenv("HF_GOOGLE_API_KEY") or os.getenv("GOOGLE_API_KEY")
|
| 11 |
if not api_key:
|
| 12 |
+
raise ValueError("Google API key not found. Please set HF_GOOGLE_API_KEY in Hugging Face Spaces secrets or GOOGLE_API_KEY in .env file.")
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
genai.configure(api_key=api_key)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error configuring Generative AI: {str(e)}")
|
| 18 |
+
raise
|
| 19 |
|
| 20 |
# Global chat sessions dictionary to maintain sessions between requests
|
| 21 |
chat_sessions = {}
|
|
|
|
| 24 |
"""Get an existing chat session or create a new one."""
|
| 25 |
if session_id not in chat_sessions:
|
| 26 |
try:
|
| 27 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 28 |
chat_sessions[session_id] = model.start_chat(history=[])
|
| 29 |
except Exception as e:
|
| 30 |
raise Exception(f"Failed to create chat session: {str(e)}")
|
| 31 |
|
| 32 |
return chat_sessions[session_id]
|
| 33 |
|
| 34 |
+
def chat_interface(message, history):
|
| 35 |
+
"""Simple interface compatible with gradio's ChatInterface."""
|
| 36 |
+
if not message or not message.strip():
|
| 37 |
+
return ""
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
+
# Default session ID
|
| 41 |
+
session_id = "default"
|
| 42 |
chat = get_chat_session(session_id)
|
| 43 |
+
response = chat.send_message(message)
|
| 44 |
return response.text
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"Error in chat_interface: {str(e)}")
|
| 47 |
+
return f"Üzgünüm, bir hata oluştu: {str(e)}"
|
| 48 |
|
| 49 |
+
# Create a simple ChatInterface instead of Blocks
|
| 50 |
+
demo = gr.ChatInterface(
|
| 51 |
+
fn=chat_interface,
|
| 52 |
+
title="ChatBot Pro - Gemini AI",
|
| 53 |
+
description="Gemini AI tabanlı bir sohbet uygulaması",
|
| 54 |
+
examples=["Merhaba, nasılsın?", "Python nedir?", "Yapay zeka hakkında bilgi verir misin?"],
|
| 55 |
+
theme="soft"
|
| 56 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Set share=True for demo purposes, but this isn't required in Hugging Face Spaces
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
try:
|
| 61 |
+
# Server settings aren't needed in Hugging Face Spaces
|
| 62 |
+
# demo.launch will be handled by Hugging Face Spaces directly
|
| 63 |
+
demo.launch()
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"Error launching Gradio app: {str(e)}")
|
cli_app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from google import generativeai as genai
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
def clear_screen():
|
| 8 |
+
"""Terminal ekranını temizler."""
|
| 9 |
+
os.system('cls' if os.name == 'nt' else 'clear')
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Configure Google API
|
| 15 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 16 |
+
if not api_key:
|
| 17 |
+
print("HATA: GOOGLE_API_KEY bulunamadı!")
|
| 18 |
+
print("Lütfen .env dosyasına API anahtarınızı ekleyin.")
|
| 19 |
+
sys.exit(1)
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
genai.configure(api_key=api_key)
|
| 23 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 24 |
+
chat = model.start_chat(history=[])
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"HATA: Gemini API bağlantısı kurulamadı: {str(e)}")
|
| 27 |
+
sys.exit(1)
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
"""Ana uygulama döngüsü."""
|
| 31 |
+
clear_screen()
|
| 32 |
+
print("=" * 60)
|
| 33 |
+
print(" CHATBOT PRO - Terminal Sürümü")
|
| 34 |
+
print("=" * 60)
|
| 35 |
+
print("Gemini AI ile sohbet etmeye başlayın.")
|
| 36 |
+
print("Çıkmak için 'q' veya 'quit' yazın.")
|
| 37 |
+
print("=" * 60)
|
| 38 |
+
|
| 39 |
+
while True:
|
| 40 |
+
user_input = input("\n\033[1mSiz:\033[0m ")
|
| 41 |
+
|
| 42 |
+
if user_input.lower() in ['q', 'quit', 'exit', 'çıkış']:
|
| 43 |
+
print("\nGörüşmek üzere!")
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
if not user_input.strip():
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
print("\n\033[1;34mChatBot Pro:\033[0m ", end="")
|
| 51 |
+
response = chat.send_message(user_input)
|
| 52 |
+
print(response.text)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"\n\033[1;31mHATA: {str(e)}\033[0m")
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
main()
|
pydub_patch.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import MagicMock
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# Create fake audioop module
|
| 5 |
+
sys.modules['audioop'] = MagicMock()
|
| 6 |
+
sys.modules['pyaudioop'] = MagicMock()
|
| 7 |
+
|
| 8 |
+
# Don't import app here to avoid circular imports
|
| 9 |
+
# The app will import this module, not the other way around
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
gradio==4.
|
| 2 |
google-generativeai==0.7.2
|
| 3 |
python-dotenv==1.0.1
|
| 4 |
-
numpy==
|
| 5 |
-
pillow==10.
|
|
|
|
| 1 |
+
gradio==4.26.0
|
| 2 |
google-generativeai==0.7.2
|
| 3 |
python-dotenv==1.0.1
|
| 4 |
+
numpy==1.24.3
|
| 5 |
+
pillow==10.0.0
|
run.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# First import the pydub patch to mock audio modules
|
| 5 |
+
import pydub_patch
|
| 6 |
+
|
| 7 |
+
# Then import and run the app
|
| 8 |
+
try:
|
| 9 |
+
from app import demo
|
| 10 |
+
|
| 11 |
+
if __name__ == "__main__":
|
| 12 |
+
print("Starting ChatBot Pro...")
|
| 13 |
+
demo.launch()
|
| 14 |
+
print("ChatBot Pro has been launched successfully!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error starting ChatBot Pro: {str(e)}")
|
| 17 |
+
sys.exit(1)
|