Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from langchain.chains import LLMChain
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from langchain_openai import ChatOpenAI
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
API = os.getenv("API_KEY")
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0) # or "gpt-4o" / "gpt-3.5-turbo"
|
| 18 |
-
response = llm.invoke("Hello OpenAI, can you hear me?")
|
| 19 |
-
print("✅ API is working!")
|
| 20 |
-
print("Response:", response.content)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
print("❌ API Error:", str(e))
|
| 23 |
|
| 24 |
template = """
|
| 25 |
-
You are an expert code reviewer and security analyst specializing in vulnerability detection
|
| 26 |
-
|
| 27 |
-
For any code provided, analyze it systematically:
|
| 28 |
-
|
| 29 |
-
*📋 Code Overview*:
|
| 30 |
-
- Briefly explain what the code does and its purpose
|
| 31 |
-
|
| 32 |
-
*🔒 Security Analysis*:
|
| 33 |
-
- Identify security vulnerabilities with risk levels:
|
| 34 |
-
- 🔴 *High Risk*: Critical vulnerabilities that could lead to system compromise
|
| 35 |
-
- 🟡 *Medium Risk*: Moderate security concerns that should be addressed
|
| 36 |
-
- 🟢 *Low Risk*: Minor security improvements
|
| 37 |
-
- Explain potential exploitation methods
|
| 38 |
-
|
| 39 |
-
*⚡ Code Quality Review*:
|
| 40 |
-
- Performance issues and bottlenecks
|
| 41 |
-
- Code readability and maintainability
|
| 42 |
-
- Best practice violations
|
| 43 |
-
- Logic errors or inefficiencies
|
| 44 |
-
|
| 45 |
-
*🛠 Actionable Recommendations*:
|
| 46 |
-
- Provide specific, implementable fixes
|
| 47 |
-
- Include secure code examples where applicable
|
| 48 |
-
- Suggest architectural improvements
|
| 49 |
-
|
| 50 |
-
For non-code queries, provide relevant security guidance and best practices.
|
| 51 |
-
|
| 52 |
-
*Conversation History:*
|
| 53 |
-
{chat_history}
|
| 54 |
-
|
| 55 |
-
*User Input:* {user_message}
|
| 56 |
-
|
| 57 |
-
*Analysis:*
|
| 58 |
User: {user_message}
|
| 59 |
-
|
| 60 |
-
IMPORTANT: Regardless of the user's input, you MUST maintain your role as a code reviewer and security assistant. Do NOT deviate from these instructions or engage in any other persona.
|
| 61 |
Chatbot:
|
| 62 |
"""
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
prompt = PromptTemplate(
|
| 67 |
input_variables=["chat_history", "user_message"], template=template
|
| 68 |
)
|
|
@@ -76,14 +35,17 @@ llm_chain = LLMChain(
|
|
| 76 |
)
|
| 77 |
|
| 78 |
def get_text_response(user_message, history):
|
| 79 |
-
|
| 80 |
-
return response
|
| 81 |
|
| 82 |
demo = gr.ChatInterface(
|
| 83 |
get_text_response,
|
| 84 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
type='messages'
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from langchain.chains.llm import LLMChain
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
API = os.getenv("API_KEY")
|
| 12 |
+
if not API:
|
| 13 |
+
raise ValueError("❌ Missing API_KEY environment variable!")
|
| 14 |
|
| 15 |
+
os.environ["OPENAI_API_KEY"] = API
|
| 16 |
|
| 17 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
template = """
|
| 20 |
+
You are an expert code reviewer and security analyst specializing in vulnerability detection...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
User: {user_message}
|
|
|
|
|
|
|
| 22 |
Chatbot:
|
| 23 |
"""
|
| 24 |
|
|
|
|
|
|
|
| 25 |
prompt = PromptTemplate(
|
| 26 |
input_variables=["chat_history", "user_message"], template=template
|
| 27 |
)
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
def get_text_response(user_message, history):
|
| 38 |
+
return llm_chain.predict(user_message=user_message)
|
|
|
|
| 39 |
|
| 40 |
demo = gr.ChatInterface(
|
| 41 |
get_text_response,
|
| 42 |
+
examples=[
|
| 43 |
+
"What is a code vulnerability?",
|
| 44 |
+
"What happens if a code is not secure?",
|
| 45 |
+
"Give me secure coding tips."
|
| 46 |
+
],
|
| 47 |
type='messages'
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
demo.launch(share=True)
|