#production level code
import gradio as gr
#function to add sql functionality only feedback loop is considered
import os
import sqlite3
import pandas as pd
import gradio as gr
import requests
import json
from gradio_client import Client
# Database file path
DB_FILE = "./reviews.db"
token = os.getenv('hf_token')
client = Client("waliasharan/demo_pri",hf_token=token)
# Establish initial connection to create the table if it doesn't exist
db = sqlite3.connect(DB_FILE)
try:
db.execute("SELECT * FROM feedback").fetchall()
#pass
except sqlite3.OperationalError:
db.execute(
'''
CREATE TABLE feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
question TEXT,
answer TEXT,
liked_q_ans TEXT,
is_liked BOOLEAN ,
review INTEGER,
comments TEXT
)
''')
db.commit()
db.close()
# Function to add a review to the database
def add_review(question: str, answer: str, type1: str, liked: bool, rating: int, comments: str):
db = sqlite3.connect(DB_FILE)
cursor = db.cursor()
cursor.execute("INSERT INTO feedback (question, answer, liked_q_ans,is_liked,review,comments) VALUES (?, ?, ?,?,?,?)", [question,answer,type1,liked,rating,comments])
db.commit()
#reviews, total_reviews = get_latest_reviews(db)
db.close()
#return "successs"
'''
list1=[]
#list2= []
# Function to handle user's response
def yes(message, history):
print(f'History value is {history}')
print(f'Message value is {message}')
list1.append([message, "random response"])
return "random response"
'''
list1=[]
# Function to handle user's response
def yes(input_text, history):
m = client.predict(input_text)
list1.append([input_text, str(m)])
return str(m)
# Function to handle like/dislike event, show feedback components, and store the index of the clicked item
def vote(data: gr.LikeData):
#print(f'Vote value: {data.value}, Index: {data.index}, Liked: {data.liked}')
print(f'Vote data: {data}')
'''
# Store question or answer feedback in list1 based on index type
if data.index[1] == 0:
list1[data.index[0]].append(['q', data.liked])
elif data.index[1] == 1:
list1[data.index[0]].append(['a', data.liked])
'''
# Show feedback components and store the index of the item being rated
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), data.index,data.liked
# Function to handle the rating and feedback submission, referencing the stored index
def submit_rating_and_feedback(rating, feedback_text, target_index,target_liked):
# Record feedback details for the specific question/answer pair
'''
if target_index is not None and target_index < len(list1):
list1[target_index].append([rating, feedback_text])
print(f"Feedback recorded for item {target_index}: Rating {rating}, Text {feedback_text}")
print(f"taget qa is {target_qa}")
'''
if target_index is not None :
if target_index[1] == 0:
list1[target_index[0]].append(['q', target_liked,rating, feedback_text])
#add here sql conn function to update feedback on questions as well since it is not needed didnt added here
elif target_index[1] == 1:
list1[target_index[0]].append(['a', target_liked,rating, feedback_text])
#list2.append((list1[target_index[0]][0],list1[target_index[0]][1],'a', target_liked,rating, feedback_text))
add_review(list1[target_index[0]][0],list1[target_index[0]][1],'a', target_liked,rating, feedback_text)
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
with gr.Blocks() as demo:
# Chatbot interface
gr.Image("POWER.png", label="Your Logo", elem_id="header_image")
chatbot = gr.Chatbot(placeholder="GenAI chatbot
Ask Me Anything about this website")
# Dropdown for feedback rating (1-10), initially hidden
rating_dropdown = gr.Dropdown(choices=[str(i) for i in range(10, 101,10)], label="Rate this response", visible=False)
# Textbox for additional feedback, initially hidden
feedback_textbox = gr.Textbox(placeholder="Enter your feedback here", visible=False, label="Additional Feedback")
# Button to submit the rating, initially hidden
submit_button = gr.Button("Submit Rating", visible=False)
# Hidden state to store the index of the item being rated
feedback_index_state = gr.State()
feedback_index_qa = gr.State()
# Attach like/dislike button event listener to show feedback components and store index in feedback_index_state
chatbot.like(vote, outputs=[rating_dropdown, submit_button, feedback_textbox, feedback_index_state,feedback_index_qa])
# Handle feedback submission and clear feedback components
submit_button.click(
submit_rating_and_feedback,
inputs=[rating_dropdown, feedback_textbox, feedback_index_state,feedback_index_qa],
outputs=[rating_dropdown, submit_button, feedback_textbox]
)
# Custom Chat Interface
chat_interface = gr.ChatInterface(
fn=yes,
textbox=gr.Textbox(placeholder="Please enter your question here", container=False, scale=7),
chatbot=chatbot,
theme="soft",
#retry_btn=None,
#undo_btn=None,
#clear_btn=None,
)
#if __name__=="__main__":
demo.launch(debug=True)