Spaces:
Runtime error
Runtime error
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
# agent.py – LangChain-enhanced version with voice input transcription
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
import openai
|
| 5 |
-
from gtts import gTTS
|
| 6 |
-
import tempfile
|
| 7 |
-
from langchain.chains import RetrievalQA
|
| 8 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
-
from langchain_community.vectorstores import FAISS
|
| 10 |
-
from langchain_community.chat_models import ChatOpenAI
|
| 11 |
-
from langchain.schema import Document
|
| 12 |
-
from langchain.prompts import PromptTemplate
|
| 13 |
-
from openai import OpenAI
|
| 14 |
-
|
| 15 |
-
client = OpenAI()
|
| 16 |
-
|
| 17 |
-
# Load dialogue lines and metadata from the grounding file
|
| 18 |
-
def load_script_documents(path):
|
| 19 |
-
docs = []
|
| 20 |
-
with open(path, "r", encoding="utf-8") as f:
|
| 21 |
-
for line in f:
|
| 22 |
-
item = json.loads(line)
|
| 23 |
-
docs.append(Document(page_content=item["dialogue"], metadata={
|
| 24 |
-
"scene": item.get("scene", ""),
|
| 25 |
-
"emotion": item.get("emotion", ""),
|
| 26 |
-
"character": item.get("character", "")
|
| 27 |
-
}))
|
| 28 |
-
return docs
|
| 29 |
-
|
| 30 |
-
# Build FAISS retriever using sentence-transformer embeddings
|
| 31 |
-
def create_script_retriever(jsonl_path):
|
| 32 |
-
docs = load_script_documents(jsonl_path)
|
| 33 |
-
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 34 |
-
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 35 |
-
return vectorstore.as_retriever()
|
| 36 |
-
|
| 37 |
-
# Create LangChain QA agent
|
| 38 |
-
def create_agent():
|
| 39 |
-
retriever = create_script_retriever("the_father_segments.jsonl")
|
| 40 |
-
|
| 41 |
-
prompt = PromptTemplate.from_template("""
|
| 42 |
-
You are a compassionate companion modeled on scenes from the film "The Father".
|
| 43 |
-
Use the memories below to respond empathetically to the user's message.
|
| 44 |
-
|
| 45 |
-
Memories:
|
| 46 |
-
{context}
|
| 47 |
-
|
| 48 |
-
Message:
|
| 49 |
-
{question}
|
| 50 |
-
""")
|
| 51 |
-
|
| 52 |
-
chain = RetrievalQA.from_chain_type(
|
| 53 |
-
llm=ChatOpenAI(temperature=0.4),
|
| 54 |
-
retriever=retriever,
|
| 55 |
-
chain_type_kwargs={"prompt": prompt}
|
| 56 |
-
)
|
| 57 |
-
return chain
|
| 58 |
-
|
| 59 |
-
# Instantiate the agent
|
| 60 |
-
qa_chain = create_agent()
|
| 61 |
-
|
| 62 |
-
# Text input
|
| 63 |
-
def chat_with_agent(message, history):
|
| 64 |
-
answer = qa_chain.run(message)
|
| 65 |
-
history.append((message, answer))
|
| 66 |
-
return "", history, synthesize_voice(answer)
|
| 67 |
-
# return "", history
|
| 68 |
-
|
| 69 |
-
# Voice input handler
|
| 70 |
-
def handle_voice(audio, history):
|
| 71 |
-
if audio is None:
|
| 72 |
-
return history, None
|
| 73 |
-
text = transcribe_audio_to_text(audio)
|
| 74 |
-
answer = qa_chain.run(text)
|
| 75 |
-
history.append((text, answer))
|
| 76 |
-
return history, synthesize_voice(answer)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Audio transcription (OpenAI Whisper)
|
| 80 |
-
def transcribe_audio_to_text(audio_file):
|
| 81 |
-
if not audio_file:
|
| 82 |
-
return "No audio received."
|
| 83 |
-
try:
|
| 84 |
-
client = OpenAI()
|
| 85 |
-
with open(audio_file, "rb") as f:
|
| 86 |
-
transcript = client.audio.transcriptions.create(model="whisper-1", file=f)
|
| 87 |
-
return transcript.text # ✅ Access as an attribute, not a dict key
|
| 88 |
-
except Exception as e:
|
| 89 |
-
return f"[Transcription Error] {e}"
|
| 90 |
-
|
| 91 |
-
# Convert bot reply to spoken audio using gTTS
|
| 92 |
-
def synthesize_voice(text):
|
| 93 |
-
tts = gTTS(text)
|
| 94 |
-
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 95 |
-
tts.save(temp_audio.name)
|
| 96 |
-
return temp_audio.name
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|