Delete index.py
Browse files
index.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import datetime
|
| 2 |
-
import threading
|
| 3 |
-
import time
|
| 4 |
-
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
from langchain_ollama import ChatOllama
|
| 7 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import os
|
| 10 |
-
from gtts import gTTS
|
| 11 |
-
import io
|
| 12 |
-
import pygame
|
| 13 |
-
|
| 14 |
-
load_dotenv()
|
| 15 |
-
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 16 |
-
llm = ChatGroq(
|
| 17 |
-
model="mixtral-8x7b-32768",
|
| 18 |
-
temperature=0.7,
|
| 19 |
-
api_key=groq_api_key,
|
| 20 |
-
verbose = False
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
prompt = ChatPromptTemplate.from_messages(
|
| 24 |
-
[
|
| 25 |
-
("system", "You are an intelligent AI assistant. Answer the questions as accurately as possible."),
|
| 26 |
-
("human", "{input}")
|
| 27 |
-
]
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
chain = prompt | llm
|
| 31 |
-
|
| 32 |
-
convo_history = []
|
| 33 |
-
audio_playing = False
|
| 34 |
-
|
| 35 |
-
def stop_audio():
|
| 36 |
-
global audio_playing
|
| 37 |
-
if audio_playing:
|
| 38 |
-
pygame.mixer.music.stop()
|
| 39 |
-
audio_playing = False
|
| 40 |
-
|
| 41 |
-
def play_tts(text):
|
| 42 |
-
global audio_playing
|
| 43 |
-
audio_playing = True
|
| 44 |
-
|
| 45 |
-
def speak_async():
|
| 46 |
-
global audio_playing
|
| 47 |
-
pygame.mixer.init()
|
| 48 |
-
fp = io.BytesIO()
|
| 49 |
-
gTTS(text).write_to_fp(fp)
|
| 50 |
-
fp.seek(0)
|
| 51 |
-
pygame.mixer.music.load(fp)
|
| 52 |
-
pygame.mixer.music.play()
|
| 53 |
-
while pygame.mixer.music.get_busy():
|
| 54 |
-
pygame.time.Clock().tick(10)
|
| 55 |
-
audio_playing = False
|
| 56 |
-
pygame.mixer.quit()
|
| 57 |
-
|
| 58 |
-
thread = threading.Thread(target=speak_async)
|
| 59 |
-
thread.start()
|
| 60 |
-
|
| 61 |
-
def ChatBot(text):
|
| 62 |
-
|
| 63 |
-
global convo_history, audio_playing
|
| 64 |
-
|
| 65 |
-
stop_audio()
|
| 66 |
-
|
| 67 |
-
convo_history.append(f"Human: {text}")
|
| 68 |
-
|
| 69 |
-
full_context = "\n".join(convo_history)
|
| 70 |
-
response = chain.invoke({"input": full_context}).content
|
| 71 |
-
|
| 72 |
-
convo_history.append(f"AI: {response}")
|
| 73 |
-
|
| 74 |
-
print("\n".join(convo_history))
|
| 75 |
-
|
| 76 |
-
play_tts(response)
|
| 77 |
-
|
| 78 |
-
return "\n".join(convo_history)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
demo = gr.Interface(
|
| 82 |
-
fn=ChatBot,
|
| 83 |
-
inputs="text",
|
| 84 |
-
outputs="text",
|
| 85 |
-
title="AudioBOT",
|
| 86 |
-
description="Ask any question to the AI. It remembers the context of the conversation and speaks its response.",
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|