Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openbot chatbot upload #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added openbot-qna-main/chroma_db/chroma.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
153 changes: 153 additions & 0 deletions openbot-qna-main/final_slow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import json
import os
import streamlit as st
import google.generativeai as gen_ai
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Configure Streamlit page settings
st.set_page_config(
page_title="OpenBot Chat",
page_icon="🔍",
layout="centered",
)

# Set up the Generative AI model
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
gen_ai.configure(api_key=GOOGLE_API_KEY)
model = gen_ai.GenerativeModel('gemini-pro')

# Load preprocessed summarized README content
@st.cache_resource
def load_preprocessed_summaries():
try:
with open('summarized_readmes.json', 'r') as f:
return json.load(f)
except Exception as e:
st.error(f"Error loading preprocessed summaries: {e}")
return {}

# Load the summarized content
summarized_readme_contents = load_preprocessed_summaries()

# Combine all summarized content into one string
combined_summary_content = "\n\n---\n\n".join([
f"Summary from {url}:\n{summary}" for url, summary in summarized_readme_contents.items()
])

# Initialize session state for chat history if not already present
if "chat_history" not in st.session_state:
st.session_state.chat_history = []

# CSS Styling for Chat UI
st.markdown("""
<style>
.response-card {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
margin-top: 10px;
color: #333;
}
.reference-text {
font-size: 12px;
color: #555;
}
.source-link {
color: #0366d6;
text-decoration: none;
margin-right: 10px;
}
.source-link:hover {
text-decoration: underline;
}
</style>
""", unsafe_allow_html=True)

# Title of the Streamlit app
st.title("🔍 OpenBot Chat")

# Checkbox for debugging and displaying the combined summary content
#if st.checkbox("Show Summarized README Content"):
# st.text_area("Combined Summarized README Content", combined_summary_content, height=200)

# User input area in the form
with st.form(key="user_input_form"):
user_input = st.text_input(
"Ask a question about OpenBot",
placeholder="e.g., What is OpenBot?",
key="user_input"
)
submit_button = st.form_submit_button("Ask")

# Function to check if the response contains a source link
def contains_source_link(response_text):
"""Check if the response contains a valid source URL."""
return "Source:" in response_text and "http" or "github.com" in response_text

# Process user input and generate response
if submit_button and user_input:
# Check if summarized content is loaded
if not combined_summary_content:
st.error("Could not load summarized README contents.")
st.stop()

# Save user input to the chat history
st.session_state.chat_history.append(("user", user_input))

# Split the summarized content into chunks if necessary (to avoid exceeding token limits)
CHUNK_SIZE = 15000 # Adjust chunk size as needed to fit within token limits
readme_chunks = [
combined_summary_content[i:i + CHUNK_SIZE]
for i in range(0, len(combined_summary_content), CHUNK_SIZE)
]

responses = []
for chunk in readme_chunks:
contextual_prompt = f"""Based on the following summarized README content chunk, please provide a detailed answer to the question. If the information comes from a specific README, include that source in your response:

{chunk}

Question: {user_input}

Please provide a comprehensive answer and cite which README file(s) the information comes from.
"""
try:
# Get the response from the AI model
response = model.start_chat(history=[]).send_message(contextual_prompt)
responses.append(response.text)
except Exception as e:
st.error(f"Error generating response for a chunk: {e}")
continue

# Filter responses to ensure valid sources are included
valid_responses = [resp for resp in responses if contains_source_link(resp)]

# If at least one response has a source, use it; otherwise, provide a fallback message
if valid_responses:
final_response = "\n\n---\n\n".join(valid_responses)
else:
final_response = "I could not find a direct answer. Try to search differently!"

# Add the final response to chat history
st.session_state.chat_history.append(("assistant", final_response))

# Display chat history (user and assistant messages)
for role, message in st.session_state.chat_history:
if role == "user":
st.markdown(f"""
<div class="response-card">
<strong>You:</strong>
<p>{message}</p>
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div class="response-card">
<strong>OpenBot:</strong>
<p>{message}</p>
</div>
""", unsafe_allow_html=True)
110 changes: 110 additions & 0 deletions openbot-qna-main/preprocessing_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
import requests
from dotenv import load_dotenv
import google.generativeai as gen_ai
from sqlalchemy import create_engine

# Load environment variables
load_dotenv()

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
gen_ai.configure(api_key=GOOGLE_API_KEY)
model = gen_ai.GenerativeModel('gemini-pro')

# Function to fetch README content
def fetch_readme_content(raw_url):
try:
response = requests.get(raw_url)
if response.status_code != 200:
return None
return response.text
except Exception as e:
return None

# Function to summarize the README content
def summarize_readme(content):
summary_prompt = f"Summarize the following README content briefly:\n\n{content}"
try:
summary_response = model.start_chat(history=[]).send_message(summary_prompt)
return summary_response.text
except Exception as e:
return None

# Function to store summaries in a database (e.g., PostgreSQL)
def store_summary_in_db(summary, url):
engine = create_engine(os.getenv("DATABASE_URL"))
with engine.connect() as connection:
connection.execute(
"INSERT INTO readme_summaries (url, summary) VALUES (%s, %s)",
(url, summary)
)

# Process the README URLs
README_URLS = {
"https://github.com/isl-org/OpenBot/blob/master/README.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/README.md",
"https://github.com/isl-org/OpenBot/blob/master/android/README.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/android/README.md",
"https://github.com/isl-org/OpenBot/blob/master/android/controller/README.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/android/controller/README.md",
"https://github.com/isl-org/OpenBot/blob/master/android/robot/README.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/android/robot/README.md",
"https://github.com/isl-org/OpenBot/blob/master/android/robot/src/main/java/org/openbot/googleServices/README.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/android/robot/src/main/java/org/openbot/googleServices/README.md",
"https://github.com/isl-org/OpenBot/blob/master/android/robot/ContributionGuide.md":
"https://raw.githubusercontent.com/isl-org/OpenBot/master/android/robot/ContributionGuide.md",
"https://github.com/ob-f/OpenBot/blob/master/body/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/cad/block_body/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/cad/block_body/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/cad/glue_body/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/cad/glue_body/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/cad/regular_body/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/cad/regular_body/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/cad/slim_body/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/cad/slim_body/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/pcb/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/pcb/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/diy/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/diy/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/lite/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/lite/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/mtv/pcb/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/mtv/pcb/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/mtv/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/mtv/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/rc_truck/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/rc_truck/README.md",
"https://github.com/ob-f/OpenBot/blob/master/body/rtr/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/body/rtr/README.md",
"https://github.com/ob-f/OpenBot/blob/master/controller/flutter/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/controller/flutter/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md",
"https://github.com/ob-f/OpenBot/blob/master/controller/flutter/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/controller/flutter/README.md",
"https://github.com/ob-f/OpenBot/blob/master/firmware/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/firmware/README.md",
"https://github.com/ob-f/OpenBot/blob/master/ios/OpenBot/OpenBot/Authentication/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/ios/OpenBot/OpenBot/Authentication/README.md",
"https://github.com/ob-f/OpenBot/blob/master/ios/OpenBot/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/ios/OpenBot/README.md",
"https://github.com/ob-f/OpenBot/blob/master/open-code/src/components/blockly/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/open-code/src/components/blockly/README.md",
"https://github.com/ob-f/OpenBot/blob/master/open-code/src/services/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/open-code/src/services/README.md",
"https://github.com/ob-f/OpenBot/blob/master/open-code/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/open-code/README.md",
"https://github.com/ob-f/OpenBot/blob/master/policy/frontend/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/policy/frontend/README.md",
"https://github.com/ob-f/OpenBot/blob/master/policy/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/policy/README.md",
"https://github.com/ob-f/OpenBot/blob/master/python/README.md":
"https://raw.githubusercontent.com/ob-f/OpenBot/master/python/README.md"
}

# Loop through each README URL and process
for display_url, raw_url in README_URLS.items():
content = fetch_readme_content(raw_url)
if content:
summary = summarize_readme(content)
if summary:
store_summary_in_db(summary, raw_url)
10 changes: 10 additions & 0 deletions openbot-qna-main/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
beautifulsoup4
langchain_google_genai
google-generativeai
langchain-community
chromadb
langchain==0.0.335
gradio==3.50.2
llama-cpp-python==0.2.18
pypdf
tenacity
58 changes: 58 additions & 0 deletions openbot-qna-main/streamlit/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[global]
# If True, will show a warning when you run a Streamlit-enabled script via "python my_script.py".
# Default: true
showWarningOnDirectExecution = true

[logger]
# Level of logging: 'error', 'warning', 'info', or 'debug'.
# Default: 'info'
level = "debug"



[runner]
# Allows you to type a variable or string by itself in a single line of Python code to write it to the app.
# Default: true
magicEnabled = true



[server]
# List of folders that should not be watched for changes. Relative paths will be taken as relative to the current working directory.
# Example: ['/home/user1/env', 'relative/path/to/folder']
# Default: []
folderWatchBlacklist = ['']

# If false, will attempt to open a browser window on start.
# Default: false unless (1) we are on a Linux box where DISPLAY is unset, or (2) server.liveSave is set.
headless = true

# Immediately share the app in such a way that enables live monitoring, and post-run analysis.
# Default: false
liveSave = false

# Automatically rerun script when the file is modified on disk.
# Default: false
runOnSave = false

# The port where the server will listen for client and browser connections.
# Default: 8501
port = 80

# Enables support for Cross-Origin Request Sharing, for added security.
# Default: true
enableCORS = false

[browser]
# Internet address of the server that the browser should connect to. Can be IP address or DNS name.
# Default: 'localhost'
serverAddress = "0.0.0.0"

# Whether to send usage statistics to Streamlit.
# Default: true
gatherUsageStats = true

# Port that the browser should use to connect to the server when in liveSave mode.
# Default: whatever value is set in server.port.
serverPort = 80

3 changes: 3 additions & 0 deletions openbot-qna-main/streamlit/credentials.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[general]
email=""

Loading