Spaces:
Runtime error
Runtime error
File size: 6,035 Bytes
d626534 cc80d64 d626534 65d425e d626534 65d425e d626534 ae5c6f8 d626534 ef9e0e1 ae5c6f8 d626534 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import streamlit as st
from streamlit_chat import message
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import CTransformers
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders.csv_loader import CSVLoader
import requests
from PIL import Image
import pydeck as pdk
import os
import json
st.set_page_config(
page_title="FoodGPT - Nagpur Based Food Recommendation System.",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
)
from langchain.document_loaders.csv_loader import CSVLoader
loader = CSVLoader(file_path='data.csv')
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50)
text_chunks = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device':"cpu"})
vector_store = FAISS.from_documents(text_chunks,embeddings)
llm = CTransformers(model="llama-2-7b-chat.ggmlv3.q4_0 10.47.24 AM.bin",model_type="llama",
config={'max_new_tokens':128,'temperature':0.01})
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
chain = ConversationalRetrievalChain.from_llm(llm=llm,chain_type='stuff',
retriever=vector_store.as_retriever(search_kwargs={"k":2}),
memory=memory)
# Sidebar for user input
st.sidebar.title("FoodGPT!π")
st.sidebar.info("FoodGPT : A Nagpur Based Food Recommendation Chat! Recommends you the best locally recognized brands for your cravings! As this system is backed with LLMA-2 on hand picked data.")
github_link = "[GitHub]('https://github.com/prasanna-muppidwar/Nagpur-FoodGPT')"
st.sidebar.info("To contribute and Sponser - " + github_link)
st.title("FoodGPT: A Nagpur based Food Recommendation Bot! π")
if 'history' not in st.session_state:
st.session_state['history'] = []
if 'generated' not in st.session_state:
st.session_state['generated'] = ["Hello! I'm FoodGPT, Ask me anything about Nagpur's Food."]
if 'past' not in st.session_state:
st.session_state['past'] = ["Hello!"]
reply_container = st.container()
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_input("Question:", placeholder="Ask anything about Nagpur's Food Joints or cravings", key='input')
image_upload = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
submit_button = st.form_submit_button(label='Send')
try:
if submit_button and user_input:
output = chain({"question": user_input, "chat_history": st.session_state['history']})["answer"]
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
if st.session_state['generated']:
with reply_container:
for i in range(len(st.session_state['generated'])):
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="thumbs")
message(st.session_state["generated"][i], key=str(i), avatar_style="fun-emoji")
import requests
import streamlit as st
import time
def query_image_classification(image_bytes, max_retries=3):
for retry in range(max_retries):
try:
response = requests.post(API_URL, headers=HEADERS, data=image_bytes)
response.raise_for_status() # Raise an error for non-2xx HTTP responses
result = response.json()
return result
except requests.exceptions.RequestException as e:
st.error(f"An error occurred during the API request: {str(e)}")
except ValueError as e:
st.error(f"An error occurred while processing the API response: {str(e)}")
if retry < max_retries - 1:
st.warning(f"Retrying request (attempt {retry + 1}/{max_retries})...")
time.sleep(1) # Wait for a moment before retrying
st.error("No classification result received after multiple retries.")
return None
if image_upload:
image_bytes = image_upload.read()
classification_result = query_image_classification(image_bytes)
if classification_result:
st.image(image_upload, caption="Uploaded Image", use_column_width=True)
if isinstance(classification_result, list) and classification_result:
# Ensure that classification_result is a list of results and not empty
best_label = max(classification_result, key=lambda x: x.get('score', 0))
if 'label' in best_label:
st.header("Image Classification Result:")
st.write(f"Classified as: {best_label['label']}")
else:
st.error("Invalid classification result format. Missing 'label' key.")
else:
st.error("Invalid classification result format or empty result list.")
import pydeck as pdk
st.title("Nagpur Map")
center = [21.1458, 79.0882]
st.pydeck_chart(
pdk.Deck(
map_style="mapbox://styles/mapbox/light-v9",
initial_view_state={
"latitude": center[0],
"longitude": center[1],
"zoom": 13,
"pitch": 10,
},
layers=[
pdk.Layer(
"ScatterplotLayer",
data=[{"position": center, "tooltip": "Nagpur"}],
get_position="position",
get_radius=10000,
get_color=[255, 0, 0],
pickable=True,
),
],
)
)
|