Spaces:
Runtime error
Runtime error
File size: 1,221 Bytes
288ac48 1d2dab8 f1a123c 288ac48 1d2dab8 288ac48 f1a123c 1d2dab8 f1a123c 1d2dab8 288ac48 1d2dab8 fa5049f f1a123c 1d2dab8 288ac48 02215e8 fa5049f 288ac48 fa5049f 288ac48 1d2dab8 288ac48 f1a123c 1d2dab8 f1a123c fa5049f 02215e8 1d2dab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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)
|