Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from langchain.chains.llm import LLMChain | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain_openai import ChatOpenAI | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| API = os.getenv("API_KEY") | |
| if not API: | |
| raise ValueError("❌ Missing API_KEY environment variable!") | |
| os.environ["OPENAI_API_KEY"] = API | |
| llm = ChatOpenAI(model="gpt-4o-mini", temperature=0) | |
| template = """ | |
| You are an expert code reviewer and security analyst specializing in vulnerability detection... | |
| User: {user_message} | |
| Chatbot: | |
| """ | |
| prompt = PromptTemplate( | |
| input_variables=["chat_history", "user_message"], template=template | |
| ) | |
| memory = ConversationBufferMemory(memory_key="chat_history") | |
| llm_chain = LLMChain( | |
| llm=llm, | |
| prompt=prompt, | |
| memory=memory | |
| ) | |
| def get_text_response(user_message, history): | |
| return llm_chain.predict(user_message=user_message) | |
| demo = gr.ChatInterface( | |
| get_text_response, | |
| examples=[ | |
| "What is a code vulnerability?", | |
| "What happens if a code is not secure?", | |
| "Give me secure coding tips." | |
| ], | |
| type='messages' | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |