|
|
import gradio as gr |
|
|
import requests |
|
|
from gtts import gTTS |
|
|
import os |
|
|
from transformers import pipeline |
|
|
import random |
|
|
|
|
|
|
|
|
API_URL = "https://api.together.xyz/v1/chat/completions" |
|
|
API_KEY = "ccf72cc06e74d9814ab60ca112c40492b4a9b9b552a88c4203ccfb6a95cebd8b" |
|
|
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1" |
|
|
|
|
|
|
|
|
asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny") |
|
|
|
|
|
|
|
|
chat_history = [] |
|
|
|
|
|
|
|
|
BIBLE_FACTS = [ |
|
|
"The Bible has around 611,000 words, depending on the translation.", |
|
|
"Psalm 119 is the longest chapter in the Bible, with 176 verses!", |
|
|
"The shortest verse in the Bible is John 11:35: ‘Jesus wept.’", |
|
|
"The word ‘Christian’ is used only three times in the whole Bible.", |
|
|
"The oldest book in the Bible is believed to be Job, not Genesis." |
|
|
] |
|
|
|
|
|
|
|
|
def get_ai_response(user_text): |
|
|
global chat_history |
|
|
|
|
|
headers = {"Authorization": f"Bearer {API_KEY}"} |
|
|
|
|
|
|
|
|
if "tell me something cool" in user_text.lower() or "bible fact" in user_text.lower(): |
|
|
return random.choice(BIBLE_FACTS) |
|
|
|
|
|
|
|
|
if len(chat_history) > 5: |
|
|
chat_history = chat_history[-5:] |
|
|
|
|
|
messages = [{"role": "system", "content": "You are ZEAL AI, a Bible-based spiritual assistant."}] |
|
|
messages += chat_history |
|
|
messages.append({"role": "user", "content": user_text}) |
|
|
|
|
|
data = {"model": MODEL_NAME, "messages": messages, "temperature": 0.9, "max_tokens": 500} |
|
|
|
|
|
response = requests.post(API_URL, json=data, headers=headers) |
|
|
try: |
|
|
response_json = response.json() |
|
|
reply = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No response") |
|
|
|
|
|
|
|
|
chat_history.append({"role": "user", "content": user_text}) |
|
|
chat_history.append({"role": "assistant", "content": reply}) |
|
|
|
|
|
return reply |
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
|
|
|
def text_to_speech(text): |
|
|
tts = gTTS(text=text, lang="en") |
|
|
audio_path = "response.mp3" |
|
|
tts.save(audio_path) |
|
|
return audio_path |
|
|
|
|
|
|
|
|
def chat_with_zeal(text_input, audio_input): |
|
|
global chat_history |
|
|
|
|
|
|
|
|
if audio_input is not None: |
|
|
asr_result = asr(audio_input) |
|
|
user_text = asr_result.get("text", "") |
|
|
else: |
|
|
user_text = text_input |
|
|
|
|
|
if not user_text: |
|
|
return gr.update(value=""), None, chat_history |
|
|
|
|
|
|
|
|
ai_response = get_ai_response(user_text) |
|
|
|
|
|
|
|
|
audio_file = text_to_speech(ai_response) |
|
|
|
|
|
|
|
|
display_history = [] |
|
|
for msg in chat_history: |
|
|
if msg["role"] == "user": |
|
|
display_history.append(("You", msg["content"])) |
|
|
else: |
|
|
display_history.append(("ZEAL AI", msg["content"])) |
|
|
|
|
|
return display_history, audio_file, gr.update(value="") |
|
|
|
|
|
|
|
|
|
|
|
custom_css = """ |
|
|
#chatbot-container { |
|
|
display: flex; |
|
|
flex-direction: column; |
|
|
height: 30vh; |
|
|
} |
|
|
#chatbot { |
|
|
flex-grow: 1; |
|
|
max-height: 30vh; /* Limits chat area height */ |
|
|
overflow-y: auto; |
|
|
border-radius: 10px; |
|
|
padding: 10px; |
|
|
} |
|
|
#input-area { |
|
|
display: flex; |
|
|
align-items: center; |
|
|
padding: 10px; |
|
|
background-color: #f0f0f0; |
|
|
border-radius: 10px; |
|
|
} |
|
|
#input-area textarea { |
|
|
flex-grow: 1; |
|
|
font-size: 16px; |
|
|
padding: 10px; |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
with gr.Blocks(css=custom_css) as demo: |
|
|
gr.Markdown("## 🤖 ZEAL AI - Bible-Based Spiritual Assistant") |
|
|
|
|
|
with gr.Row(elem_id="chatbot-container"): |
|
|
chatbot = gr.Chatbot(label="ZEAL AI Chat") |
|
|
|
|
|
with gr.Row(elem_id="input-area"): |
|
|
text_input = gr.Textbox(label="Type your message:") |
|
|
audio_input = gr.Audio(label="Or record your message:", type="filepath") |
|
|
send_btn = gr.Button("Send") |
|
|
|
|
|
audio_output = gr.Audio(label="ZEAL AI Says (Audio)", autoplay=True) |
|
|
chat_history_state = gr.State([]) |
|
|
|
|
|
|
|
|
send_btn.click( |
|
|
chat_with_zeal, |
|
|
inputs=[text_input, audio_input], |
|
|
outputs=[chatbot, audio_output, text_input] |
|
|
) |
|
|
|
|
|
|
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|