Spaces:
Sleeping
Sleeping
Make requirment and support python file
Browse files- chatbot.py +132 -0
- requirment.txt +9 -0
- utils.py +38 -0
chatbot.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 6 |
+
import edge_tts
|
| 7 |
+
import asyncio
|
| 8 |
+
import os
|
| 9 |
+
from typing import Optional
|
| 10 |
+
GROQ_API_KEY = "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"
|
| 11 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class CodeAssistantBot:
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.client = Groq(api_key=GROQ_API_KEY)
|
| 18 |
+
self.model = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.6)
|
| 19 |
+
|
| 20 |
+
# Initialize prompts
|
| 21 |
+
self.analysis_prompt = ChatPromptTemplate.from_messages([
|
| 22 |
+
("system",
|
| 23 |
+
"""You are an expert code assistant. Analyze the code and context provided,
|
| 24 |
+
then give clear, helpful responses. Keep responses concise and focused on the code."""
|
| 25 |
+
),
|
| 26 |
+
("user", """Code: {code}
|
| 27 |
+
Output: {output}
|
| 28 |
+
Error: {error}
|
| 29 |
+
Question: {question}""")
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
self.summary_prompt = ChatPromptTemplate.from_messages([(
|
| 33 |
+
"system",
|
| 34 |
+
"""Summarize the conversation focusing on key technical points and insights.
|
| 35 |
+
Keep it brief and clear."""
|
| 36 |
+
), ("user", "Conversation: {conversation}")])
|
| 37 |
+
|
| 38 |
+
def analyze_code(self, code: str, output: str, error: str,
|
| 39 |
+
question: str) -> str:
|
| 40 |
+
try:
|
| 41 |
+
parser = StrOutputParser()
|
| 42 |
+
chain = self.analysis_prompt | self.model | parser
|
| 43 |
+
return chain.invoke({
|
| 44 |
+
'code': code,
|
| 45 |
+
'output': output,
|
| 46 |
+
'error': error,
|
| 47 |
+
'question': question
|
| 48 |
+
})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Sorry, I encountered an error: {str(e)}"
|
| 51 |
+
|
| 52 |
+
def summarize_conversation(self, conversation: list) -> str:
|
| 53 |
+
try:
|
| 54 |
+
parser = StrOutputParser()
|
| 55 |
+
chain = self.summary_prompt | self.model | parser
|
| 56 |
+
formatted_conv = "\n".join(
|
| 57 |
+
[f"Q: {q}\nA: {a}" for q, a in conversation])
|
| 58 |
+
return chain.invoke({'conversation': formatted_conv})
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Could not generate summary: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
async def text_to_speech(text: str, filename: str):
|
| 64 |
+
voice = "en-US-AriaNeural"
|
| 65 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 66 |
+
await communicate.save(filename)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def render_chatbot(code: str, output: str, error: str):
|
| 70 |
+
"""Render the chatbot UI as an integrated panel"""
|
| 71 |
+
|
| 72 |
+
# Initialize session state
|
| 73 |
+
if "conversation" not in st.session_state:
|
| 74 |
+
st.session_state.conversation = []
|
| 75 |
+
if "audio_count" not in st.session_state:
|
| 76 |
+
st.session_state.audio_count = 0
|
| 77 |
+
|
| 78 |
+
# Create bot instance
|
| 79 |
+
bot = CodeAssistantBot()
|
| 80 |
+
|
| 81 |
+
# Chat interface
|
| 82 |
+
st.markdown("""
|
| 83 |
+
<style>
|
| 84 |
+
.chat-message {
|
| 85 |
+
padding: 10px;
|
| 86 |
+
border-radius: 5px;
|
| 87 |
+
margin-bottom: 10px;
|
| 88 |
+
}
|
| 89 |
+
.user-message {
|
| 90 |
+
background-color: #e3f2fd;
|
| 91 |
+
}
|
| 92 |
+
.bot-message {
|
| 93 |
+
background-color: #f5f5f5;
|
| 94 |
+
}
|
| 95 |
+
</style>
|
| 96 |
+
""",
|
| 97 |
+
unsafe_allow_html=True)
|
| 98 |
+
|
| 99 |
+
# Chat history in a scrollable container
|
| 100 |
+
chat_container = st.container()
|
| 101 |
+
with chat_container:
|
| 102 |
+
for q, a in st.session_state.conversation:
|
| 103 |
+
st.markdown(
|
| 104 |
+
f'<div class="chat-message user-message">**You:** {q}</div>',
|
| 105 |
+
unsafe_allow_html=True)
|
| 106 |
+
st.markdown(
|
| 107 |
+
f'<div class="chat-message bot-message">**Assistant:** {a}</div>',
|
| 108 |
+
unsafe_allow_html=True)
|
| 109 |
+
|
| 110 |
+
# Input area
|
| 111 |
+
user_input = st.text_input("Ask about your code...",
|
| 112 |
+
key="chat_input",
|
| 113 |
+
placeholder="Type your question here...")
|
| 114 |
+
|
| 115 |
+
if user_input:
|
| 116 |
+
# Get response
|
| 117 |
+
response = bot.analyze_code(code, output, error, user_input)
|
| 118 |
+
st.session_state.conversation.append((user_input, response))
|
| 119 |
+
|
| 120 |
+
# Generate summary and speech if conversation is long enough
|
| 121 |
+
if len(st.session_state.conversation) > 3:
|
| 122 |
+
with st.spinner("Generating conversation summary..."):
|
| 123 |
+
summary = bot.summarize_conversation(
|
| 124 |
+
st.session_state.conversation)
|
| 125 |
+
audio_file = f"summary_{st.session_state.audio_count}.wav"
|
| 126 |
+
asyncio.run(text_to_speech(summary, audio_file))
|
| 127 |
+
st.session_state.audio_count += 1
|
| 128 |
+
|
| 129 |
+
# Display summary in a collapsible section
|
| 130 |
+
with st.expander("📝 Conversation Summary", expanded=False):
|
| 131 |
+
st.markdown(summary)
|
| 132 |
+
st.audio(audio_file, format="audio/wav")
|
requirment.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
streamlit-ace
|
| 3 |
+
groq
|
| 4 |
+
langchain
|
| 5 |
+
langchain-core
|
| 6 |
+
langchain-groq
|
| 7 |
+
edge-tts
|
| 8 |
+
audio_recorder_streamlit
|
| 9 |
+
python-dotenv
|
utils.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from io import StringIO
|
| 3 |
+
import contextlib
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
@contextlib.contextmanager
|
| 8 |
+
def capture_output():
|
| 9 |
+
"""Capture stdout and stderr"""
|
| 10 |
+
stdout, stderr = StringIO(), StringIO()
|
| 11 |
+
old_out, old_err = sys.stdout, sys.stderr
|
| 12 |
+
try:
|
| 13 |
+
sys.stdout, sys.stderr = stdout, stderr
|
| 14 |
+
yield stdout, stderr
|
| 15 |
+
finally:
|
| 16 |
+
sys.stdout, sys.stderr = old_out, old_err
|
| 17 |
+
|
| 18 |
+
def execute_code(code: str) -> tuple:
|
| 19 |
+
"""
|
| 20 |
+
Execute the provided code and return stdout, stderr and any exceptions
|
| 21 |
+
"""
|
| 22 |
+
with capture_output() as (stdout, stderr):
|
| 23 |
+
try:
|
| 24 |
+
exec(code)
|
| 25 |
+
return stdout.getvalue().strip(), stderr.getvalue().strip(), None
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return stdout.getvalue().strip(), stderr.getvalue().strip(), str(e)
|
| 28 |
+
|
| 29 |
+
def export_session(code: str, output: str, error: str) -> dict:
|
| 30 |
+
"""
|
| 31 |
+
Create an exportable session object
|
| 32 |
+
"""
|
| 33 |
+
return {
|
| 34 |
+
"timestamp": datetime.now().isoformat(),
|
| 35 |
+
"code": code,
|
| 36 |
+
"output": output,
|
| 37 |
+
"error": error
|
| 38 |
+
}
|