Skip to content

Commit

Permalink
Merge branch 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Devparihar5 committed Dec 14, 2023
2 parents 35cd7ec + 9331b35 commit 8e2969a
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# .gitignore

# Python
__pycache__/

# Virtual Environment
venv/
gem/

# IDE files
.idea/
.vscode/

# Compiled Python files
*.pyc
*.pyo
*.pyd

# Python cache
*.pyc

# Jupyter Notebook
.ipynb_checkpoints/
39 changes: 39 additions & 0 deletions Gemini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import streamlit as st
import pandas as pd

# Data provided
data = {
'Capability': ['MMLU', 'CoT@32*', 'Big-Bench Hard', 'DROP', 'HellaSwag', 'GSM8K', 'maj1@32', 'MATH', 'HumanEval', 'Natural2Code'],
'Benchmark': ['Representation of questions in 57 subjects', 'Reasoning', 'Reading comprehension (F1 Score)', 'Commonsense reasoning for everyday tasks', 'Basic arithmetic manipulations', 'Basic arithmetic manipulations', 'Python code generation', 'Challenging math problems', 'Python code generation', 'Python code generation'],
'Higher is better': [90.0, 86.4, 83.6, 82.4, 87.8, 94.4, 92.0, 53.2, 74.4, 74.9],
'Description': ['CoT@32*', '5-shot** (reported)', '3-shot', 'Variable shots', '10-shot*', 'maj1@32', '5-shot CoT (reported)', '4-shot', '0-shot (IT)*', '0-shot']
}
# Create a DataFrame from the data
df = pd.DataFrame(data)

# Page Title
st.title("Google's Gemini Model Guide 🚀🤖")

# Introduction
st.header("Introduction to Google's Gemini Model")
st.sidebar.markdown("[Why Gemini?](#why-gemini)")

st.write("""
Welcome to the forefront of cutting-edge AI with Google's Gemini model! 🚀🤖
In this comprehensive guide, we'll dive deep into the intricacies of leveraging the power of GenerativeAI through Gemini. From seamless installation steps to fine-tuning configurations, and exploring the myriad benefits, this guide is your key to unlocking the full potential of this groundbreaking model.
""")

# Why Gemini?
st.header("🌐 Why Gemini?")
st.sidebar.markdown("[Installation](#installation)")

st.write("""
Gemini stands at the intersection of innovation and AI prowess. Its capabilities extend beyond the ordinary, promising a new era of generative content creation that's both powerful and dynamic.
""")
st.table(df)
# Learn More section
st.header("📚 Learn More")
# Add a clickable link
st.markdown("""
For a deeper understanding and additional insights, check out accompanying [blog post](https://deepmind.google/technologies/gemini/#introduction) dedicated to Google's Gemini Model. Let's embark on this exciting journey together! 🌟🔍.
""", unsafe_allow_html=True)
Binary file added images/image-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/text-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions pages/1 Gemini Pro-text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import google.generativeai as genai
import streamlit as st

# Function to initialize session state
def initialize_session_state():
return st.session_state.setdefault('api_key', None)

# Main Streamlit app
def text_page():
st.title("Gemini NexusCraft")

# Initialize session state
initialize_session_state()

# Configure API key
api_key = st.sidebar.text_input("Enter your API key:", value=st.session_state.api_key)

# Check if the API key is provided
if not api_key:
st.sidebar.error("Please enter your API key.")
st.stop()
else:
# Store the API key in session state
st.session_state.api_key = api_key

genai.configure(api_key=api_key)


# Set up the model configuration options
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.9, 0.1)
top_p = st.sidebar.number_input("Top P", 0.0, 1.0, 1.0, 0.1)
top_k = st.sidebar.number_input("Top K", 1, 100, 1)
max_output_tokens = st.sidebar.number_input("Max Output Tokens", 1, 5000, 2048)

# Set up the model
generation_config = {
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"max_output_tokens": max_output_tokens,
}


safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
prompt = st.text_input("Enter your Query:")
# Check if the query is provided
if not prompt:
st.error("Please enter your query.")
st.stop()

if st.button("Ask Gemini"):
gemini = genai.GenerativeModel(model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)


prompt_parts = [prompt]

response = gemini.generate_content(prompt_parts)
# st.write(response.text)
st.write(response.text)

# Run the Streamlit app
if __name__ == "__main__":
text_page()
163 changes: 163 additions & 0 deletions pages/2 Gemini Pro Vision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import google.generativeai as genai
import streamlit as st
import json
import requests
import re

# Function to initialize session state
def initialize_session_state():
return st.session_state.setdefault('api_key', None)

# Main Streamlit app
def home():
st.title("Gemini NexusCraft")

# Initialize session state
initialize_session_state()

# Configure API key
api_key = st.sidebar.text_input("Enter your API key:", value=st.session_state.api_key)

# Check if the API key is provided
if not api_key:
st.sidebar.error("Please enter your API key.")
st.stop()
else:
# Store the API key in session state
st.session_state.api_key = api_key

# Check if the API key is provided
if not api_key:
st.sidebar.error("Please enter your API key.")
st.stop()
genai.configure(api_key=api_key)


model = 'gemini-pro-vision'
# Sidebar option to choose image source
image_source = st.sidebar.radio("Choose Image Source:", ["URL", "Upload"])
prompt = st.text_input("Enter your Query:")
if image_source == "URL":
# Input for image URL
image_url = st.sidebar.text_input("Enter the image URL:")
# Prepare content for Gemini model
contents = [
{
"parts": [
{
"text":prompt
},
{
"image": {
"image_url": image_url
}
}
]
}
]

else:
# Input for uploading an image
image_url = st.sidebar.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])


# Prepare content for Gemini model
contents = [
{
"parts": [
{
"text":prompt
},
{
"image": {
"uploaded_file": image_url
}
}
]
}
]

# Set up the model configuration options
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.9, 0.1)
top_p = st.sidebar.number_input("Top P", 0.0, 1.0, 1.0, 0.1)
top_k = st.sidebar.number_input("Top K", 1, 100, 1)
max_output_tokens = st.sidebar.number_input("Max Output Tokens", 1, 5000, 2048)

# Set up the model
generation_config = {
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"max_output_tokens": max_output_tokens,
}

safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
# Check if the query is provided
if not prompt:
st.error("Please enter your query.")
st.stop()

# Generate content and display the response
if st.button("Ask Gemini"):

# generation_config = json.loads(generation_config)
# safety_settings = json.loads(safety_settings)
stream = False
for content in contents:
for n, part in enumerate(content['parts']):
if image:=part.get('image', None):
if uploaded_file := image.get('uploaded_file', None):
data = uploaded_file.read()
mime_type = uploaded_file.type
elif image_url:=image.get('image_url', None):
response = requests.get(image_url)
data = response.content
mime_type = response.headers['content-type']
else:
raise ValueError('Either uploaded_file or image_url must be provided.')

if mime_type is None:
# Guess!
mime_type = 'image/png'

blob = {'data': data, 'mime_type': mime_type}
content['parts'][n] = blob
st.image(data)
gemini = genai.GenerativeModel(model_name=model)

response = gemini.generate_content(
contents,
generation_config=generation_config,
safety_settings=safety_settings,
stream=False)
st.subheader("Gemini Model Response:")
# st.write(response.candidates)
# st.write(response.candidates[0])
data_str = str(response.candidates[0])

# Use regular expressions to extract the text part
match = re.search(r'text: "(.*?)"', data_str)
if match:
extracted_text = match.group(1)
st.write(extracted_text)

# Run the Streamlit app
if __name__ == "__main__":
home()
56 changes: 56 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Gemini NexusCraft

This project consists of two Streamlit applications that leverage the capabilities of the Gemini AI model provided by Google's GenerativeAI. The Gemini model is a powerful text and image generation model that can be used for various creative and informative purposes.

## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Text Generation](#text-generation)
- [Text and Image Generation](#text-and-image-generation)
- [Configuration](#configuration)
- [Contributing](#contributing)
- [License](#license)

## Installation

1. Clone the repository:

```bash
git clone https://github.com/Devparihar5/Gemini-NexusCraft.git
cd Gemini-NexusCraft
```

2. Install the required dependencies:

```bash
pip install -r requirements.txt
```

## Usage

### Text Generation

To run the Streamlit app for text generation, execute the following command:

```bash
streamlit run Gemini.py
```

This app allows you to interact with the Gemini model by providing a text prompt. It also provides configuration options such as temperature, top_p, top_k, and max_output_tokens.

### Text and Image Generation


## Configuration

In both apps, you need to enter your API key to authenticate with the Gemini model. The API key can be entered through the Streamlit sidebar. Make sure to enter a valid API key to access the model.

Additionally, you can configure various model parameters such as temperature, top_p, top_k, and max_output_tokens to control the generation process.

## Contributing

Contributions to this project are welcome. If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request.

## License

This project is licensed under the [MIT License](LICENSE). Feel free to use and modify the code for your own purposes.
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
google-generativeai==0.3.1
pandas==2.1.4
streamlit==1.29.0

0 comments on commit 8e2969a

Please sign in to comment.