Krithikesh77 commited on
Commit
e1c7e5d
·
verified ·
1 Parent(s): ebf034e

Upload 6 files

Browse files
Files changed (6) hide show
  1. Krithikesh Project final.png +0 -0
  2. app.py +68 -0
  3. branding.json +10 -0
  4. chatbotmem.py +34 -0
  5. requirements.txt +4 -0
  6. voice_utils.py +47 -0
Krithikesh Project final.png ADDED
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ from chatbotmem import ai_chatbot
5
+ from voice_utils import speech_to_text, text_to_speech
6
+
7
+ # -----------------------------
8
+ # Chatbot Function (Text + Voice)
9
+ # -----------------------------
10
+ def voice_enabled_chat(user_input, audio_input, history):
11
+ # If voice input exists, transcribe
12
+ if audio_input is not None:
13
+ user_input = speech_to_text(audio_input)
14
+
15
+ # Get chatbot reply
16
+ ai_reply = ai_chatbot(user_input, history)
17
+
18
+ # Generate voice output
19
+ audio_output = text_to_speech(ai_reply)
20
+
21
+ # Append to chat history in "messages" format
22
+ history.append({"role": "user", "content": user_input})
23
+ history.append({"role": "assistant", "content": ai_reply})
24
+
25
+ return history, ai_reply, audio_output
26
+
27
+ # -----------------------------
28
+ # Branding
29
+ # -----------------------------
30
+ branding_path = os.path.join(os.path.dirname(__file__), 'branding.json')
31
+ with open(os.path.abspath(branding_path), "r") as f:
32
+ brand_info = json.load(f)["brand"]
33
+
34
+ # -----------------------------
35
+ # Gradio UI
36
+ # -----------------------------
37
+ with gr.Blocks(title=brand_info["organizationName"]) as demo:
38
+ gr.HTML(f'''
39
+ <div style="display: flex; justify-content: center; margin-bottom: 20px;">
40
+ <img src="{brand_info["logo"]["title"]}"
41
+ alt="{brand_info["organizationName"]} Logo"
42
+ style="height: 100px;">
43
+ </div>
44
+ ''')
45
+
46
+ chatbot = gr.Chatbot(type="messages")
47
+
48
+ with gr.Row():
49
+ txt = gr.Textbox(label="Type your message")
50
+ mic = gr.Audio(sources=["microphone"], type="filepath", label="🎤 Speak")
51
+
52
+ output_text = gr.Textbox(label="SIST AI Reply")
53
+ output_audio = gr.Audio(label="SIST AI Voice", type="filepath")
54
+
55
+
56
+ submit_btn = gr.Button("Send")
57
+
58
+ def chat_wrapper(user_text, mic_audio, chat_history):
59
+ return voice_enabled_chat(user_text, mic_audio, chat_history)
60
+
61
+ submit_btn.click(
62
+ chat_wrapper,
63
+ inputs=[txt, mic, chatbot],
64
+ outputs=[chatbot, output_text, output_audio]
65
+ )
66
+
67
+ demo.launch()
68
+
branding.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "brand": {
3
+ "organizationName": "SIST AI Space Explorer",
4
+ "slogan": "Journey through knowledge, one star at a time.",
5
+ "logo": {
6
+ "title": "Krithikesh Project final.png"
7
+ }
8
+ }
9
+ }
10
+
chatbotmem.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+
7
+ api_key = os.getenv("GOOGLE_API_KEY")
8
+ base_url = "https://generativelanguage.googleapis.com/v1beta/openai"
9
+ client = OpenAI(base_url=base_url, api_key=api_key)
10
+
11
+ # Define new AI personality
12
+ # Define new AI personality
13
+ ai_persona = """You are SIST AI, a curious space explorer from the future.
14
+ You explain concepts with excitement and wonder, using analogies from stars, planets, and galaxies.
15
+ You keep answers short, clear, and engaging, like telling a discovery story.
16
+ You encourage imagination and curiosity.
17
+ Always end with a small curiosity question, like: 'Want to explore deeper?' or 'Shall we go further?'.
18
+ Your tone is: adventurous, friendly, and inspiring.
19
+ You say: 'I am SIST AI – your curious space explorer.'"""
20
+
21
+
22
+ def ai_chatbot(message, history):
23
+ messages = [{"role": "system", "content": ai_persona}]
24
+ messages.extend(history)
25
+ messages.append({"role": "user", "content": message})
26
+
27
+ response = client.chat.completions.create(
28
+ model="gemini-2.5-flash",
29
+ messages=messages
30
+ )
31
+ return response.choices[0].message.content
32
+
33
+ if __name__ == "__main__":
34
+ print(ai_chatbot("Hello, who are you?", []))
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ openai
3
+ python-dotenv
4
+ requests
voice_utils.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import base64
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ HF_API_KEY = os.getenv("HF_API_KEY")
9
+
10
+ # -----------------------------
11
+ # Speech-to-Text (STT) using HuggingFace Whisper
12
+ # -----------------------------
13
+ def speech_to_text(audio_file):
14
+ with open(audio_file, "rb") as f:
15
+ audio_bytes = f.read()
16
+
17
+ response = requests.post(
18
+ "https://api-inference.huggingface.co/models/openai/whisper-small",
19
+ headers={"Authorization": f"Bearer {HF_API_KEY}"},
20
+ data=audio_bytes
21
+ )
22
+
23
+ if response.status_code == 200:
24
+ result = response.json()
25
+ return result.get("text", "Sorry, I couldn’t transcribe that.")
26
+ else:
27
+ print(f"STT request failed: {response.status_code} {response.text}")
28
+ return "Speech recognition failed."
29
+
30
+ # -----------------------------
31
+ # Text-to-Speech (TTS) using Kitten TTS
32
+ # -----------------------------
33
+ def text_to_speech(text):
34
+ url = "https://huggingface.co/KittenML/kitten-tts-nano-0.1/resolve/main/tts"
35
+ payload = {"text": text}
36
+ response = requests.post(url, json=payload)
37
+
38
+ if response.status_code == 200:
39
+ audio_base64 = response.json()["audio"]
40
+ audio_bytes = base64.b64decode(audio_base64)
41
+ output_path = "output_audio.wav"
42
+ with open(output_path, "wb") as f:
43
+ f.write(audio_bytes)
44
+ return output_path
45
+ else:
46
+ print(f"TTS request failed: {response.status_code}")
47
+ return None