w1r4 commited on
Commit
42e7ec7
·
1 Parent(s): 3d3ee76
.gitignore ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual Environment
24
+ venv/
25
+ env/
26
+ ENV/
27
+
28
+ # IDE
29
+ .idea/
30
+ .vscode/
31
+ *.swp
32
+ *.swo
33
+
34
+ # OS
35
+ .DS_Store
36
+ Thumbs.db
37
+
38
+ # Logs
39
+ *.log
40
+ logs/
.huggingface/CODEOWNERS ADDED
@@ -0,0 +1 @@
 
 
1
+ * @${OWNER}
HUGGINGFACE_DEPLOYMENT.md ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Deploying to Hugging Face Spaces
2
+
3
+ This guide will help you deploy your Gemma-2 Multimodal Chat application to Hugging Face Spaces.
4
+
5
+ ## Prerequisites
6
+
7
+ 1. A [Hugging Face account](https://huggingface.co/join)
8
+ 2. A [Hugging Face API token](https://huggingface.co/settings/tokens) with write access
9
+ 3. Git installed on your machine
10
+
11
+ ## Deployment Options
12
+
13
+ You have two options for deploying to Hugging Face Spaces:
14
+
15
+ ### Option 1: Using the Provided Script (Recommended)
16
+
17
+ We've created a helper script that automates the deployment process:
18
+
19
+ 1. Run the script:
20
+ ```bash
21
+ python push_to_huggingface.py
22
+ ```
23
+
24
+ 2. Follow the prompts to enter your:
25
+ - Hugging Face username
26
+ - Space name (or accept the default "gemma-chat")
27
+ - Hugging Face API token
28
+ - Email address (for git configuration)
29
+ - Commit message
30
+
31
+ 3. The script will:
32
+ - Create a new Space if it doesn't exist
33
+ - Initialize git if needed
34
+ - Configure git credentials
35
+ - Add and commit your files
36
+ - Push to Hugging Face Spaces
37
+
38
+ 4. Once complete, your application will be available at:
39
+ `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
40
+
41
+ ### Option 2: Manual Deployment
42
+
43
+ If you prefer to deploy manually:
44
+
45
+ 1. Create a new Space on [Hugging Face Spaces](https://huggingface.co/spaces)
46
+ - Choose "Gradio" as the SDK
47
+ - Set a name for your Space
48
+
49
+ 2. Initialize git in your project directory (if not already done):
50
+ ```bash
51
+ git init
52
+ ```
53
+
54
+ 3. Configure git:
55
+ ```bash
56
+ git config --local user.name "YOUR_USERNAME"
57
+ git config --local user.email "YOUR_EMAIL"
58
+ ```
59
+
60
+ 4. Add Hugging Face as a remote:
61
+ ```bash
62
+ git remote add space https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
63
+ ```
64
+
65
+ 5. Add, commit, and push your files:
66
+ ```bash
67
+ git add .
68
+ git commit -m "Initial commit"
69
+ git push --force space main
70
+ ```
71
+
72
+ ## Configuration Files
73
+
74
+ The following files are used for Hugging Face Spaces configuration:
75
+
76
+ - `README.space.md`: The README displayed on your Space's page
77
+ - `app_config.yaml`: Configuration for your Space (model, hardware, etc.)
78
+ - `.gitignore`: Prevents unnecessary files from being uploaded
79
+
80
+ ## Troubleshooting
81
+
82
+ - **Authentication Issues**: Make sure your token has write access
83
+ - **Push Errors**: Check if your Space already exists and you have the correct URL
84
+ - **Deployment Failures**: Check the Spaces logs for error messages
85
+
86
+ ## Resources
87
+
88
+ - [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces)
89
+ - [Gradio Documentation](https://www.gradio.app/docs/)
README.md CHANGED
@@ -1,6 +1,6 @@
1
- # 🚀 Gemma-3 Multimodal Chat Application
2
 
3
- A sophisticated Gradio-based chat application featuring multimodal capabilities with Google's Gemma-3 model (placeholder implementation).
4
 
5
  ## ✨ Features
6
 
@@ -14,7 +14,7 @@ A sophisticated Gradio-based chat application featuring multimodal capabilities
14
  ## 🛠️ Technologies Used
15
 
16
  - **Frontend**: Gradio 4.0+
17
- - **AI Model**: Gemma-3 (placeholder implementation)
18
  - **File Processing**: PyPDF2 for PDFs, PIL for images
19
  - **Backend**: Python with Hugging Face Transformers
20
  - **Deployment**: Hugging Face Spaces
@@ -73,11 +73,7 @@ A sophisticated Gradio-based chat application featuring multimodal capabilities
73
  ## 🔧 Configuration
74
 
75
  ### Model Configuration
76
- The application currently uses a placeholder for Gemma-3. To integrate the actual model:
77
-
78
- 1. Replace the `gemma_3_placeholder_inference` function in `app.py`
79
- 2. Add proper model loading and inference logic
80
- 3. Update dependencies if needed
81
 
82
  ### Customization
83
  - Modify the UI theme in the `gr.Blocks` configuration
@@ -97,7 +93,7 @@ gemma/
97
 
98
  ## 🔮 Future Enhancements
99
 
100
- - [ ] Integration with actual Gemma-3 model
101
  - [ ] Support for additional file formats (DOCX, XLSX)
102
  - [ ] Advanced image processing capabilities
103
  - [ ] User authentication and personalized chat history
@@ -129,4 +125,4 @@ If you encounter any issues or have questions, please open an issue on the repos
129
 
130
  ---
131
 
132
- **Note**: This application currently uses a placeholder implementation for Gemma-3. Replace the placeholder functions with actual model integration when the model becomes available.
 
1
+ # 🚀 Gemma-2 Multimodal Chat Application
2
 
3
+ A sophisticated Gradio-based chat application featuring multimodal capabilities with Google's Gemma-2 model.
4
 
5
  ## ✨ Features
6
 
 
14
  ## 🛠️ Technologies Used
15
 
16
  - **Frontend**: Gradio 4.0+
17
+ - **AI Model**: Google's Gemma-2-2B-IT
18
  - **File Processing**: PyPDF2 for PDFs, PIL for images
19
  - **Backend**: Python with Hugging Face Transformers
20
  - **Deployment**: Hugging Face Spaces
 
73
  ## 🔧 Configuration
74
 
75
  ### Model Configuration
76
+ The application uses Google's Gemma-2-2B-IT model from Hugging Face. The model is loaded and used for inference in the `gemma_3_inference` function in `app.py`.
 
 
 
 
77
 
78
  ### Customization
79
  - Modify the UI theme in the `gr.Blocks` configuration
 
93
 
94
  ## 🔮 Future Enhancements
95
 
96
+ - [ ] Upgrade to Gemma-3 model when available
97
  - [ ] Support for additional file formats (DOCX, XLSX)
98
  - [ ] Advanced image processing capabilities
99
  - [ ] User authentication and personalized chat history
 
125
 
126
  ---
127
 
128
+ **Note**: This application uses Google's Gemma-2-2B-IT model. The model doesn't have native vision capabilities, but the application is designed to handle image uploads with appropriate messaging.
README.space.md ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gemma-2 Multimodal Chat
3
+ description: A sophisticated Gradio-based chat application featuring multimodal capabilities with Google's Gemma-2 model.
4
+ emoji: 🚀
5
+ colorFrom: blue
6
+ colorTo: purple
7
+ sdk: gradio
8
+ sdk_version: 4.0.0
9
+ python_version: 3.9
10
+ pinned: false
11
+ license: mit
12
+ ---
13
+
14
+ # 🚀 Gemma-2 Multimodal Chat Application
15
+
16
+ A sophisticated Gradio-based chat application featuring multimodal capabilities with Google's Gemma-2 model.
17
+
18
+ ## ✨ Features
19
+
20
+ - 💬 **Interactive Chat Interface**: Persistent conversation history with context awareness
21
+ - 🖼️ **Vision Capabilities**: Upload and analyze images (note: Gemma-2 doesn't have native vision capabilities)
22
+ - 📄 **File Processing**: Support for PDF and TXT file uploads with text extraction
23
+ - 🧠 **Contextual Responses**: Maintains conversation context for follow-up questions
24
+ - 🎨 **Modern UI**: Clean, responsive interface built with Gradio
25
+
26
+ ## 📖 How to Use
27
+
28
+ ### Basic Chat
29
+ 1. Type your message in the text input box
30
+ 2. Click "Submit" or press Enter
31
+ 3. View the AI response in the chat history
32
+
33
+ ### Image Analysis
34
+ 1. Upload an image using the image upload component
35
+ 2. Type a question about the image
36
+ 3. Submit to get AI-powered image analysis (note: Gemma-2 doesn't have native vision capabilities)
37
+
38
+ ### File Processing
39
+ 1. Upload a PDF or TXT file using the file upload component
40
+ 2. Ask questions about the file content
41
+ 3. The extracted text will be used as context for responses
42
+
43
+ ### Advanced Features
44
+ - **Persistent Context**: Previous conversations are remembered
45
+ - **File Context**: Uploaded file content persists for follow-up questions
46
+ - **Clear Chat**: Reset conversation history and uploaded files
47
+
48
+ ## 🛠️ Technologies Used
49
+
50
+ - **Frontend**: Gradio 4.0+
51
+ - **AI Model**: Google's Gemma-2-2B-IT
52
+ - **File Processing**: PyPDF2 for PDFs, PIL for images
53
+ - **Backend**: Python with Hugging Face Transformers
54
+
55
+ ## 📄 License
56
+
57
+ This project is licensed under the MIT License.
app.py CHANGED
@@ -40,7 +40,7 @@ def load_gemma_model():
40
 
41
  def gemma_3_inference(prompt_text: str, pil_image: Optional[Image.Image] = None, chat_history: Optional[List[Tuple[str, str]]] = None) -> str:
42
  """
43
- Real Gemma model inference function.
44
  """
45
  global model, tokenizer
46
 
@@ -205,7 +205,7 @@ def clear_chat(chat_history: List[Tuple[str, str]], file_context: str) -> Tuple[
205
  return [], "", None, None, ""
206
 
207
  # Create Gradio interface
208
- with gr.Blocks(title="Gemma-3 Multimodal Chat", theme=gr.themes.Soft()) as demo:
209
  gr.Markdown(
210
  """
211
  # 🚀 Gemma-2 Multimodal Chat Application
@@ -276,7 +276,7 @@ with gr.Blocks(title="Gemma-3 Multimodal Chat", theme=gr.themes.Soft()) as demo:
276
  - Upload either an image OR a file per message
277
  - PDF files will have their text extracted
278
  - File content persists as context for follow-up questions
279
- - Images are processed with vision capabilities
280
  """
281
  )
282
 
 
40
 
41
  def gemma_3_inference(prompt_text: str, pil_image: Optional[Image.Image] = None, chat_history: Optional[List[Tuple[str, str]]] = None) -> str:
42
  """
43
+ Gemma-2 model inference function.
44
  """
45
  global model, tokenizer
46
 
 
205
  return [], "", None, None, ""
206
 
207
  # Create Gradio interface
208
+ with gr.Blocks(title="Gemma-2 Multimodal Chat", theme=gr.themes.Soft()) as demo:
209
  gr.Markdown(
210
  """
211
  # 🚀 Gemma-2 Multimodal Chat Application
 
276
  - Upload either an image OR a file per message
277
  - PDF files will have their text extracted
278
  - File content persists as context for follow-up questions
279
+ - Note: Gemma-2 doesn't have native vision capabilities, but you can still upload images and ask text-based questions about them
280
  """
281
  )
282
 
app_config.yaml CHANGED
@@ -1,4 +1,4 @@
1
- title: Gemma-3 Multimodal Chat
2
  emoji: 🚀
3
  colorFrom: blue
4
  colorTo: purple
@@ -17,6 +17,6 @@ tags:
17
  - gradio
18
  python_version: 3.9
19
  models:
20
- - google/gemma-2b-it
21
  hardware: cpu-basic
22
  suggested_storage: small
 
1
+ title: Gemma-2 Multimodal Chat
2
  emoji: 🚀
3
  colorFrom: blue
4
  colorTo: purple
 
17
  - gradio
18
  python_version: 3.9
19
  models:
20
+ - google/gemma-2-2b-it
21
  hardware: cpu-basic
22
  suggested_storage: small
push_to_huggingface.bat ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ echo.
3
+ echo 🚀 Pushing Gemma-2 Multimodal Chat to Hugging Face Spaces
4
+ echo.
5
+
6
+ REM Check if Python is installed
7
+ python --version >nul 2>&1
8
+ if %ERRORLEVEL% NEQ 0 (
9
+ echo ❌ Python is not installed or not in PATH. Please install Python and try again.
10
+ exit /b 1
11
+ )
12
+
13
+ REM Run the Python script
14
+ python push_to_huggingface.py
15
+
16
+ if %ERRORLEVEL% EQU 0 (
17
+ echo.
18
+ echo ✅ Deployment script completed successfully!
19
+ echo.
20
+ echo Press any key to exit...
21
+ pause >nul
22
+ ) else (
23
+ echo.
24
+ echo ❌ Deployment script encountered an error.
25
+ echo.
26
+ echo Press any key to exit...
27
+ pause >nul
28
+ )
push_to_huggingface.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+
5
+ def push_to_huggingface():
6
+ """
7
+ Script to help push the Gemma-2 Multimodal Chat application to Hugging Face Spaces.
8
+ """
9
+ print("\n🚀 Pushing Gemma-2 Multimodal Chat to Hugging Face Spaces\n")
10
+
11
+ # Check if git is installed
12
+ try:
13
+ subprocess.run(["git", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
14
+ except (subprocess.SubprocessError, FileNotFoundError):
15
+ print("❌ Git is not installed or not in PATH. Please install Git and try again.")
16
+ return False
17
+
18
+ # Check if huggingface_hub is installed
19
+ try:
20
+ import huggingface_hub
21
+ except ImportError:
22
+ print("📦 Installing huggingface_hub...")
23
+ subprocess.run([sys.executable, "-m", "pip", "install", "huggingface_hub"], check=True)
24
+ import huggingface_hub
25
+
26
+ # Get Hugging Face username and space name
27
+ username = input("Enter your Hugging Face username: ").strip()
28
+ space_name = input("Enter a name for your Hugging Face Space (default: gemma-chat): ").strip() or "gemma-chat"
29
+
30
+ # Full space name
31
+ full_space_name = f"{username}/{space_name}"
32
+ print(f"\n🔍 Space will be created at: https://huggingface.co/spaces/{full_space_name}")
33
+
34
+ # Get Hugging Face token
35
+ token = input("Enter your Hugging Face token (create one at https://huggingface.co/settings/tokens): ").strip()
36
+ if not token:
37
+ print("❌ Token is required to push to Hugging Face Spaces.")
38
+ return False
39
+
40
+ # Create or check if space exists
41
+ try:
42
+ from huggingface_hub import HfApi, create_repo
43
+
44
+ api = HfApi(token=token)
45
+
46
+ # Check if space already exists
47
+ try:
48
+ api.repo_info(repo_id=full_space_name, repo_type="space")
49
+ print(f"ℹ️ Space {full_space_name} already exists. Will push to existing space.")
50
+ except Exception:
51
+ # Create new space
52
+ print(f"🆕 Creating new Hugging Face Space: {full_space_name}")
53
+ create_repo(
54
+ repo_id=space_name,
55
+ token=token,
56
+ repo_type="space",
57
+ space_sdk="gradio",
58
+ private=False
59
+ )
60
+ except Exception as e:
61
+ print(f"❌ Error creating/checking space: {str(e)}")
62
+ return False
63
+
64
+ # Initialize git if not already initialized
65
+ if not os.path.exists(".git"):
66
+ print("🔄 Initializing git repository...")
67
+ subprocess.run(["git", "init"], check=True)
68
+
69
+ # Configure git
70
+ print("🔄 Configuring git...")
71
+ subprocess.run(["git", "config", "--local", "user.name", username], check=True)
72
+ email = input("Enter your email for git configuration: ").strip()
73
+ if email:
74
+ subprocess.run(["git", "config", "--local", "user.email", email], check=True)
75
+
76
+ # Add Hugging Face as remote
77
+ print("🔄 Adding Hugging Face as remote...")
78
+ try:
79
+ subprocess.run(["git", "remote", "remove", "space"], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
80
+ except subprocess.SubprocessError:
81
+ pass # Ignore if remote doesn't exist
82
+
83
+ subprocess.run(["git", "remote", "add", "space", f"https://huggingface.co/spaces/{full_space_name}"], check=True)
84
+
85
+ # Add files to git
86
+ print("🔄 Adding files to git...")
87
+ subprocess.run(["git", "add", "."], check=True)
88
+
89
+ # Commit changes
90
+ print("🔄 Committing changes...")
91
+ commit_message = input("Enter a commit message (default: Initial commit): ").strip() or "Initial commit"
92
+ subprocess.run(["git", "commit", "-m", commit_message], check=True)
93
+
94
+ # Configure git credentials
95
+ print("🔄 Configuring git credentials...")
96
+ credential_helper = f"!f() {{ echo username={username}; echo password={token}; }}; f"
97
+ subprocess.run(["git", "config", "--local", "credential.helper", credential_helper], check=True)
98
+
99
+ # Push to Hugging Face
100
+ print("\n🚀 Pushing to Hugging Face Spaces...")
101
+ try:
102
+ subprocess.run(["git", "push", "--force", "space", "main"], check=True)
103
+ print("\n✅ Successfully pushed to Hugging Face Spaces!")
104
+ print(f"🌐 Your application is now available at: https://huggingface.co/spaces/{full_space_name}")
105
+ return True
106
+ except subprocess.SubprocessError as e:
107
+ print(f"\n❌ Error pushing to Hugging Face Spaces: {str(e)}")
108
+ print("Please check your token and try again.")
109
+ return False
110
+
111
+ if __name__ == "__main__":
112
+ push_to_huggingface()