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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "gemini-2.0-flash-exp" # Keep the model ID as is
|
| 8 |
+
try:
|
| 9 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 10 |
+
model_id = MODEL_ID
|
| 11 |
+
genai.configure(api_key=api_key)
|
| 12 |
+
except Exception as e:
|
| 13 |
+
st.error(f"Error: {e}")
|
| 14 |
+
st.stop
|
| 15 |
+
|
| 16 |
+
model = genai.GenerativeModel(MODEL_ID)
|
| 17 |
+
chat = model.start_chat()
|
| 18 |
+
|
| 19 |
+
def download_pdf():
|
| 20 |
+
"""
|
| 21 |
+
Downloads the PDF file from the Hugging Face Hub using the correct repo path and filename.
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 25 |
+
repo_id = "louiecerv/build_ml_huggingface_tutorial_dataset" # Corrected dataset repo path
|
| 26 |
+
filename = "Deploy_Apps_on_Hugging_Face.pdf"
|
| 27 |
+
filepath = hf_hub_download(repo_id=repo_id, filename=filename, token=hf_token, repo_type="dataset")
|
| 28 |
+
return filepath
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Failed to download PDF from Hugging Face Hub: {e}")
|
| 31 |
+
st.stop() # Stop if the download fails
|
| 32 |
+
|
| 33 |
+
# Initialize conversation history in Streamlit session state
|
| 34 |
+
if "conversation_history" not in st.session_state:
|
| 35 |
+
st.session_state.conversation_history = []
|
| 36 |
+
if "uploaded_file_part" not in st.session_state: # Store the file *part*
|
| 37 |
+
st.session_state.uploaded_file_part = None
|
| 38 |
+
if "uploaded_pdf_path" not in st.session_state:
|
| 39 |
+
st.session_state.uploaded_pdf_path = download_pdf()
|
| 40 |
+
|
| 41 |
+
def multimodal_prompt(pdf_path, text_prompt):
|
| 42 |
+
"""
|
| 43 |
+
Sends a multimodal prompt to Gemini, handling file uploads efficiently.
|
| 44 |
+
Args:
|
| 45 |
+
pdf_path: The path to the PDF file.
|
| 46 |
+
text_prompt: The text prompt for the model.
|
| 47 |
+
Returns:
|
| 48 |
+
The model's response as a string, or an error message.
|
| 49 |
+
"""
|
| 50 |
+
try:
|
| 51 |
+
if st.session_state.uploaded_file_part is None: # First time, upload
|
| 52 |
+
pdf_part = genai.upload_file(pdf_path, mime_type="application/pdf")
|
| 53 |
+
st.session_state.uploaded_file_part = pdf_part
|
| 54 |
+
prompt = [text_prompt, pdf_part] # First turn includes the actual file
|
| 55 |
+
else: # Subsequent turns, reference the file
|
| 56 |
+
|
| 57 |
+
prompt = [text_prompt, st.session_state.uploaded_file_part] # Subsequent turns include the file reference
|
| 58 |
+
|
| 59 |
+
response = chat.send_message(prompt)
|
| 60 |
+
|
| 61 |
+
# Update conversation history
|
| 62 |
+
st.session_state.conversation_history.append({"role": "user", "content": text_prompt, "has_pdf": True})
|
| 63 |
+
st.session_state.conversation_history.append({"role": "assistant", "content": response.text})
|
| 64 |
+
return response.text
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"An error occurred: {e}"
|
| 68 |
+
|
| 69 |
+
def display_download_button(file_path, file_name):
|
| 70 |
+
try:
|
| 71 |
+
with open(file_path, "rb") as f:
|
| 72 |
+
file_bytes = f.read()
|
| 73 |
+
b64 = base64.b64encode(file_bytes).decode()
|
| 74 |
+
href = f'<a href="data:application/pdf;base64,{b64}" download="{file_name}">Download the source document (PDF)</a>'
|
| 75 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 76 |
+
except FileNotFoundError:
|
| 77 |
+
st.error("File not found for download.")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error during download: {e}")
|
| 80 |
+
|
| 81 |
+
# Define the ML Models
|
| 82 |
+
tasks = ["About Building ML Apps on Huggingface",
|
| 83 |
+
"Uploading the Dataset",
|
| 84 |
+
"Creating and Deploying the Application",
|
| 85 |
+
"Develop the ML Application",
|
| 86 |
+
"Push the Application to Hugging Face",
|
| 87 |
+
"Deploy and Test the Application"]
|
| 88 |
+
|
| 89 |
+
# --- Sidebar ---
|
| 90 |
+
st.sidebar.title("🤖 Visual Q and A")
|
| 91 |
+
selected_task = st.sidebar.selectbox("Select a task", tasks)
|
| 92 |
+
|
| 93 |
+
# --- Main Page ---
|
| 94 |
+
st.title(" Buiding Machine Learning Apps with Hugging Face 🤗")
|
| 95 |
+
about = """
|
| 96 |
+
|
| 97 |
+
**How to use this App**
|
| 98 |
+
This app leverages Gemini 2.0 to provide insights on the provided document.
|
| 99 |
+
Select a question from the dropdown menu or enter your own question to get
|
| 100 |
+
Gemini's generated response based on the provided document.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
with st.expander("How to use this App"):
|
| 104 |
+
st.markdown(about)
|
| 105 |
+
|
| 106 |
+
# --- Q and A Tab ---
|
| 107 |
+
st.header("Questions and Answers")
|
| 108 |
+
|
| 109 |
+
# Generate 5 questions based on the selected model
|
| 110 |
+
if selected_task == "About Building ML Apps on Huggingface":
|
| 111 |
+
questions = [
|
| 112 |
+
"What are the advantages of using separate repositories for datasets, models, and applications on Hugging Face?",
|
| 113 |
+
"How does modularity and reusability benefit ML app development on Hugging Face?",
|
| 114 |
+
"Explain the role of version control and collaboration in Hugging Face repositories.",
|
| 115 |
+
"What scalability and performance features does Hugging Face Spaces offer?",
|
| 116 |
+
"How does the Hugging Face community and ecosystem enhance ML app development?",
|
| 117 |
+
"Describe the steps to upload a dataset to Hugging Face.",
|
| 118 |
+
"What are the key components of creating and deploying an ML app on Hugging Face?",
|
| 119 |
+
"How can you set up a development environment for building ML apps on Hugging Face?",
|
| 120 |
+
"What is the process for developing and pushing an application to Hugging Face Spaces?",
|
| 121 |
+
"How does documentation and discoverability improve the usability of datasets, models, and applications on Hugging Face?"
|
| 122 |
+
]
|
| 123 |
+
if selected_task == "Uploading the Dataset":
|
| 124 |
+
questions = [
|
| 125 |
+
"Provide a step-by-step guide, including Python code if needed, to upload a dataset to Hugging Face."
|
| 126 |
+
]
|
| 127 |
+
if selected_task == "Creating and Deploying the Application":
|
| 128 |
+
questions = [
|
| 129 |
+
"Provide a step-by-step guide, including Python code if needed, to create and deploy an application on Hugging Face."
|
| 130 |
+
]
|
| 131 |
+
if selected_task == "Develop the ML Application":
|
| 132 |
+
questions = [
|
| 133 |
+
"Provide a step-by-step guide, including Python code if needed, to develop an ML application on Hugging Face."
|
| 134 |
+
]
|
| 135 |
+
if selected_task == "Push the Application to Hugging Face":
|
| 136 |
+
questions = [
|
| 137 |
+
"Provide a step-by-step guide, including Python code if needed, to push the application to Hugging Face."
|
| 138 |
+
]
|
| 139 |
+
if selected_task == "Deploy and Test the Application":
|
| 140 |
+
questions = [
|
| 141 |
+
"Provide a step-by-step guide, including Python code if needed, to deploy and test the application on Hugging Face."
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
# Create a selection box
|
| 145 |
+
selected_question = st.selectbox("Choose a question", questions)
|
| 146 |
+
|
| 147 |
+
# Display a checkbox
|
| 148 |
+
if st.checkbox('Check this box to ask a question not listed above'):
|
| 149 |
+
# If the checkbox is checked, display a text box
|
| 150 |
+
selected_question = st.text_input('Enter a question')
|
| 151 |
+
|
| 152 |
+
if st.button("Ask AI"):
|
| 153 |
+
with st.spinner("AI is thinking..."):
|
| 154 |
+
if st.session_state.uploaded_pdf_path is None:
|
| 155 |
+
st.session_state.uploaded_pdf_path = download_pdf()
|
| 156 |
+
|
| 157 |
+
filepath = st.session_state.uploaded_pdf_path
|
| 158 |
+
text_prompt = f"Use the provided document. Focus on the task: {selected_task} to answer the following question: {selected_question}. Use your own knowledge as well as sources from the web and the provided document. Always cite your sourcss."
|
| 159 |
+
response = multimodal_prompt(filepath, text_prompt) # Use the downloaded filepath
|
| 160 |
+
st.markdown(f"**Question:** {selected_question}")
|
| 161 |
+
st.markdown(f"**Response:** {response}")
|
| 162 |
+
|
| 163 |
+
if st.session_state.uploaded_pdf_path:
|
| 164 |
+
display_download_button(st.session_state.uploaded_pdf_path, "Deploy_Apps_on_Hugging_Face.pdf")
|
| 165 |
+
|
| 166 |
+
st.markdown("[Visit our Hugging Face Space!](https://huggingface.co/wvsuaidev)")
|
| 167 |
+
st.markdown("© 2025 WVSU AI Dev Team 🤖 ✨")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
huggingface_hub
|
| 3 |
+
google-generativeai
|