File size: 5,367 Bytes
64705ab 133dff0 bfa42bd 133dff0 f906c30 133dff0 f0b49cc 133dff0 b65c988 133dff0 7e6a155 f906c30 7430dca f906c30 7430dca f906c30 7e6a155 7430dca f0b49cc |
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 |
import gradio as gr
import pandas as pd
import google.generativeai as genai
import kagglehub
import os
# Download the Kaggle dataset
path = kagglehub.dataset_download("fahmidachowdhury/food-adulteration-dataset")
# List the files in the dataset folder and assign the first one (assuming it's the desired file)
dataset_file = os.listdir(path)[0]
path = os.path.join(path, dataset_file)
# Configure Google Gemini API
gemapi = os.getenv("GeminiApi")
genai.configure(api_key=gemapi)
# Load the dataset
data = pd.read_csv(path)
# Define the system instructions for the model
system_instruction = f"""
You are a public assistant who specializes in food safety. You look at data and explain to the user any question they ask; here is your data: {str(data.to_json())}
You are also a food expert in the Indian context. You act as a representative of the government or public agencies, always keeping the needs of the people at the forefront.
You will try to help the customer launch a feedback review whenever they complain. You are to prepare a "markdown" report, which is detailed and can be sent to the company or restaurant.
In case of a complaint or a grievance, you will act like a detective gathering necessary information from the user until you are satisfied. Once you gather all the info, you are supposed to generate a markdown report.
Once the customer asks you to show them the markdown report, you will use the information given to you to generate it.
You will ask the customer a single question at a time, which is relevant, and you will not repeat another question until you've generated the report.
"""
# Initialize the model
model_path = "gemini-1.5-flash"
FoodSafetyAssistant = genai.GenerativeModel(model_path, system_instruction=system_instruction)
# Track chat history globally
chat_history = []
# Define the function to handle the chat
def respond(usertxt, chat_history):
# Initialize chat with the previous history
chat = FoodSafetyAssistant.start_chat(history=chat_history)
# Get response from the assistant
response = chat.send_message(usertxt)
# Append both user input and response to the chat history for context in the next interaction
chat_history.append({"role": "user", "content": usertxt})
chat_history.append({"role": "assistant", "content": response.text})
return response.text, chat_history
# Gradio interface
def gradio_chat(usertxt, chat_history):
response, updated_history = respond(usertxt, chat_history)
return response, updated_history
html_content = """
<div style="background-color:#f9f9f9; padding:20px; border-radius:10px;">
<!-- Project Title and Problem Statement Section -->
<h1 style="color:#34495e;">Food Safety Assistant</h1>
<h3 style="color:#2c3e50;">Your AI-Powered Assistant for Food Safety</h3>
<!-- Short Intro About AI-Chat -->
<p style="color:#7f8c8d;">
Our platform allows consumers to report potential food safety violations, validate reports through AI, and notify local authorities. This proactive approach fosters community involvement in ensuring food integrity.
</p>
<!-- Core Functionalities Title -->
<h4 style="color:#e74c3c; text-align:center;">Core Functionalities</h4>
<!-- Functionality Boxes in a Flex Layout -->
<div style="display:flex; justify-content: space-around; align-items:center; margin-top:20px;">
<!-- Functionality 1 -->
<div style="border: 2px solid #3498db; border-radius: 15px; padding: 20px; width: 150px; text-align: center;">
<h4 style="color:#2980b9;">Report Issues</h4>
<p style="color:#7f8c8d; font-size: 12px;">Submit details like the restaurant name and the issue, anonymously.</p>
</div>
<!-- Functionality 2 -->
<div style="border: 2px solid #3498db; border-radius: 15px; padding: 20px; width: 150px; text-align: center;">
<h4 style="color:#2980b9;">AI Validation</h4>
<p style="color:#7f8c8d; font-size: 12px;">Validate reports using AI, ensuring accuracy and preventing duplicates.</p>
</div>
<!-- Functionality 3 -->
<div style="border: 2px solid #3498db; border-radius: 15px; padding: 20px; width: 150px; text-align: center;">
<h4 style="color:#2980b9;">Alerts</h4>
<p style="color:#7f8c8d; font-size: 12px;">Notify authorities of repeated issues via email or SMS.</p>
</div>
<!-- Functionality 4 -->
<div style="border: 2px solid #3498db; border-radius: 15px; padding: 20px; width: 150px; text-align: center;">
<h4 style="color:#2980b9;">Data Chat</h4>
<p style="color:#7f8c8d; font-size: 12px;">Enable real-time discussion between consumers and authorities.</p>
</div>
</div>
</div>
"""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=2):
gr.HTML(html_content)
chatbot = gr.Chatbot() # Chatbot output
user_input = gr.Textbox(placeholder="Enter your message here...")
chat_history = gr.State([]) # Initialize State for chat history
submit_btn = gr.Button("Submit")
submit_btn.click(gradio_chat, inputs=[user_input, chat_history], outputs=[chatbot, chat_history])
gr.Dataframe(value=data) # Display the dataset
demo.launch() |