Skip to content

Commit 973ff40

Browse files
committed
hw-4 assignment complete
1 parent 67a1fb3 commit 973ff40

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
.env
3+
4+
stock-data-with-yfinance/apple_stock_1y_history.csv
5+
6+
stock-data-with-yfinance/apple-1yr-stock-graph.png
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 🤖 AI Chat Bot with Google Gemini API
2+
3+
Welcome to the **AI Chat Bot - Google Gemini** project!
4+
This Python program uses the Google Gemini API to extract text from images, its a terminal-based application with a short conversational user interface.
5+
6+
---
7+
8+
## 🚀 Key Features
9+
10+
- **Conversational User Interface:** Engages the user with prompts for their name and an image URL.
11+
- **Dynamic Image Handling:** Automatically detects the image type (e.g., PNG, JPEG) from the URL, making it flexible for different image formats.
12+
- **Robust URL Fetching:** Includes error checking to ensure the provided URL is valid and the image can be downloaded successfully.
13+
- **AI-Powered Text Extraction:** Leverages the Google Gemini API to accurately perform Optical Character Recognition (OCR) on the image.
14+
- **Personalized Output:** Appends the user's name to the extracted text for a custom touch.
15+
16+
---
17+
18+
## 🗝️ Requires a Google AI Studio API Key
19+
20+
To use this project, you must have a valid [Google AI Studio API Key](https://aistudio.google.com/apikey).
21+
This key is required to authenticate requests to the Gemini API.
22+
Store your API key securely in a `.env` file as described below.
23+
24+
### 🔑 How to Create a Google AI Studio API Key
25+
26+
1. Go to [Google AI Studio API Keys](https://aistudio.google.com/apikey).
27+
2. Sign in with your Google account if prompted.
28+
3. Click the **"Create API Key"** button.
29+
4. Copy the generated API key.
30+
5. In your project folder, create a file named `.env` (if it doesn't exist).
31+
6. Add the following line to your `.env` file (replace with your actual key):
32+
```
33+
GOOGLE_API_KEY=your-google-api-key-here
34+
```
35+
**🙏IMPORTANT: Never share your API key publicly or commit it to version control.**
36+
37+
---
38+
39+
## 🛠️ How to Run
40+
41+
1. **Clone the repository**
42+
43+
```bash
44+
git clone https://github.com/your-username/Python-Programs.git
45+
cd Python-Programs/ai-chat-bot-google-gemini
46+
```
47+
48+
2. **Set up your environment variables**
49+
50+
- Create a `.env` file in this folder with:
51+
```
52+
GOOGLE_API_KEY=your-google-api-key-here # Replace with your actual API key
53+
```
54+
55+
3. **Install dependencies**
56+
57+
```bash
58+
pip install -r requirements.txt
59+
```
60+
61+
4. **Run the program in your terminal**
62+
```bash
63+
python main.py
64+
```
65+
66+
---
67+
68+
## 📂 Folder Structure
69+
70+
```
71+
ai-chat-bot-google-gemini/
72+
73+
├── main.py # Main Python script
74+
├── requirements.txt # Python dependencies
75+
├── .env # Environment variables (not tracked by git)
76+
└── README.md # This documentation
77+
```
78+
79+
---
80+
81+
## 📚 References
82+
83+
- [Google Gemini API Documentation](https://ai.google.dev/)
84+
- [Google Generative AI Python SDK](https://github.com/google-gemini/generative-ai-python)
85+
- [Google AI Studio API Key](https://aistudio.google.com/apikey)
86+
- [Google AI Image Understanding](https://ai.google.dev/gemini-api/docs/image-understanding)
87+
- [python-dotenv Documentation](https://pypi.org/project/python-dotenv/)
88+
- [requests Documentation](https://docs.python-requests.org/)
89+
90+
---
91+
92+
## 🙏 Acknowledgements
93+
94+
This project was created for educational purposes and as a demonstration of integrating Google Gemini's API with Python as an assignment for the CIDM 4310/5310 Business Intelligence and Decision Support Systems course at West Texas A&M University, under the guidance of Dr. Cheng (Carl) Zhang.
95+
96+
Special thanks to the open-source community and the authors of the libraries used!
97+
98+
---
99+
100+
## 📝 License
101+
102+
This project is for educational and demonstration purposes only.

ai-chat-bot-google-gemini/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from google import genai
2+
from google.genai import types
3+
from dotenv import load_dotenv
4+
import os
5+
import requests
6+
7+
load_dotenv()
8+
g_api_key = os.getenv("GOOGLE_API_KEY")
9+
10+
print("Google Gemini API - Image Text Extraction")
11+
12+
user_name = input("Hello, what shall I call you? ")
13+
14+
print(f"Welcome {user_name}, let's extract text from an image!")
15+
16+
# Test image URL for OCR
17+
# https://github.com/czhangwt/CIDM4310/blob/main/testocr.png?raw=true
18+
# https://raw.githubusercontent.com/czhangwt/CIDM4310/refs/heads/main/testocr.png
19+
image_path = input("Please enter the image URL: ")
20+
21+
# Fetch the image from the URL
22+
image_response = requests.get(image_path, stream=True)
23+
24+
# Check if image was fetched successfully without errors
25+
image_response.raise_for_status()
26+
27+
# Finds the image format from response request results
28+
mime_type = image_response.headers.get('Content-Type')
29+
30+
# Create a Part object from the image bytes
31+
image = types.Part.from_bytes(data=image_response.content, mime_type=mime_type)
32+
33+
client = genai.Client(api_key=g_api_key)
34+
35+
response = client.models.generate_content(
36+
model="gemini-2.0-flash",
37+
contents=[
38+
# image part object for request
39+
f"Extract the text from the image, put my name {user_name} at the end of the text in a new line.", image
40+
],
41+
)
42+
43+
print(response.text)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
python-dotenv
3+
4+
google-genai
5+
6+
requests
7+
8+

0 commit comments

Comments
 (0)