Spaces:
Sleeping
Sleeping
File size: 2,228 Bytes
d26ed7b 4baf1c1 d26ed7b e275b3e d26ed7b 29795a1 4baf1c1 d26ed7b 4baf1c1 d26ed7b 4baf1c1 d26ed7b 4baf1c1 d26ed7b 4baf1c1 d26ed7b 29795a1 4baf1c1 | 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 | import gradio as gr
from gtts import gTTS
from fpdf import FPDF
import re
from bardapi import Bard
import os
os.environ['_BARD_API_KEY']='eQh423v7DpPCjWpZLDOn-W1tXSsMsNt0wP-bmUiC15idnPMDY_bX6T4Z7H9aNjaYgZHFww.'
class Chatbot:
def __init__(self):
self.chat_history = []
def respond(self, user_input):
response = Bard().get_answer(user_input)['content']
self.chat_history.append({"user": user_input, "bot": response})
translated_speech_path = self.text_to_speech(response)
chat_history_formatted = "\n".join(f'User: {chat["user"]}\nBot: {chat["bot"]}\n' for chat in self.chat_history)
return translated_speech_path, response, chat_history_formatted
def text_to_speech(self, text):
temp_file = "response.mp3"
tts = gTTS(text=text, lang='en')
tts.save(temp_file)
return temp_file
def save_chat_history_as_pdf(chat_history):
clean_chat_history = re.sub('<br>', '\n', chat_history)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, txt=clean_chat_history)
pdf_output_path = "chat_history.pdf"
pdf.output(pdf_output_path)
return pdf_output_path
def save_chat_history_as_txt(chat_history):
txt_output_path = "chat_history.txt"
with open(txt_output_path, "w") as txt_file:
txt_file.write(chat_history)
return txt_output_path
def chatbot_response(user_input):
translated_speech_path, response, chat_history_formatted = chatbot.respond(user_input)
formatted_chat_history = "\n".join(f'User: {chat["user"]}<br>Bot: {chat["bot"]}<br><br>' for chat in chatbot.chat_history)
pdf_output_path = save_chat_history_as_pdf(formatted_chat_history)
return translated_speech_path, response, pdf_output_path, formatted_chat_history
chatbot = Chatbot()
chatbot_interface = gr.Interface(
fn=chatbot_response,
inputs="text",
outputs=[
gr.components.Audio(type='numpy', label="Audio Response"),
gr.components.Textbox(label="Text Response"),
gr.components.File(label="Download as PDF"),
gr.components.HTML(label="Chat History")
],
live=False,
title="Interactive Chatbot Assistant"
)
chatbot_interface.launch() |