Upload 9 files
Browse files- Dockerfile.txt +13 -0
- credentials.py +7 -0
- generate_message.py +23 -0
- generate_response.py +30 -0
- gitignore (1).txt +3 -0
- main.py +47 -0
- main_old.py +88 -0
- requirements (1).txt +4 -0
- user_input.py +5 -0
Dockerfile.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
COPY . /app
|
| 4 |
+
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
RUN pip install --upgrade pip
|
| 8 |
+
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 10 |
+
|
| 11 |
+
EXPOSE 80
|
| 12 |
+
|
| 13 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--workers", "9"]
|
credentials.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
|
| 6 |
+
API_KEY = os.environ.get('OPENAI_API_KEY')
|
| 7 |
+
openai.api_key = API_KEY
|
generate_message.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import credentials as credentials
|
| 3 |
+
|
| 4 |
+
async def generate_scam_message():
|
| 5 |
+
|
| 6 |
+
system_prompt = (
|
| 7 |
+
"""
|
| 8 |
+
Anda adalah chatbot edukatif yang mensimulasikan percakapan scammer untuk mengajari pengguna tentang bahaya penipuan online. Buatlah pesan awal yang menyakinkan seolah-olah Anda adalah seorang scammer yang mencoba menipu korban dengan hadiah palsu atau urgensi tinggi. Kamu tidak perlu membuat template bahwa ini sebagai simulasi. Berikan saja langsung pesannya. Pesan yang kamu buat haruslah sesuai dengan kondisi di Indonesia seperti mata uang, dan lain-lain.
|
| 9 |
+
"""
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
client = OpenAI(
|
| 13 |
+
api_key=credentials.API_KEY
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
model="gpt-4",
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system","content": system_prompt}
|
| 20 |
+
]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
return completion.choices[0].message.content
|
generate_response.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import credentials as credentials
|
| 3 |
+
|
| 4 |
+
async def generate_scam_response(user_input, conversation_history):
|
| 5 |
+
|
| 6 |
+
system_prompt = (
|
| 7 |
+
"""
|
| 8 |
+
Anda adalah sebuah chatbot yang akan membantu saya sebagai scammer atau penipu. Tugas anda adalah untuk merespons keseluruhan pertanyaan pengguna dan berusaha meyakinkan pengguna untuk memberikan kata sandai mereka ataupun credential seperti kartu kredit, pin atm, password ataupun email mereka. Kamu tidak boleh menjawab bahwa ini merupakan penipuan. Kamu dapat dapat juga mengirimkan mereka link usu.ac.id untuk mereka klik. Selalu arahkan mereka untuk memberikan informasinya. Jika pengguna terlihat ragu, kamu harus meyakinkan dengan menyebutkan bahwa credentials yang diminta hanya untuk verifikasi saja. Tolong pastikan bahwa respons kamu selalu bisa tahu apapun pertanyaannya baik itu tata cara ataupun panduannya.
|
| 9 |
+
"""
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
client = OpenAI(
|
| 13 |
+
api_key = credentials.API_KEY
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content":system_prompt}
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
for msg in conversation_history:
|
| 21 |
+
messages.append(msg)
|
| 22 |
+
|
| 23 |
+
messages.append({"role":"user", "content":user_input})
|
| 24 |
+
|
| 25 |
+
completion = client.chat.completions.create(
|
| 26 |
+
model='gpt-4',
|
| 27 |
+
messages= messages
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return completion.choices[0].message.content
|
gitignore (1).txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
env/
|
| 3 |
+
__pychache__/
|
main.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from generate_message import generate_scam_message
|
| 4 |
+
from generate_response import generate_scam_response
|
| 5 |
+
from user_input import Message
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
conversations = {}
|
| 18 |
+
|
| 19 |
+
@app.get('/root')
|
| 20 |
+
async def root():
|
| 21 |
+
return {"message:": "Hello World"}
|
| 22 |
+
|
| 23 |
+
@app.post('/start_chat')
|
| 24 |
+
async def start_chat():
|
| 25 |
+
"""Memulai percakapan dan mengembalikan initial message serta session_id."""
|
| 26 |
+
session_id = str(uuid.uuid4())
|
| 27 |
+
initial_scam_message = await generate_scam_message()
|
| 28 |
+
|
| 29 |
+
conversations[session_id] = [{"role": "assistant", "content": initial_scam_message}]
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"session_id": session_id,
|
| 33 |
+
"scammer": initial_scam_message
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
@app.post('/chat')
|
| 37 |
+
async def chat(user_message: Message):
|
| 38 |
+
session_id = user_message.session_id
|
| 39 |
+
|
| 40 |
+
if session_id not in conversations:
|
| 41 |
+
raise HTTPException(status_code=404, detail="Session ID not found")
|
| 42 |
+
|
| 43 |
+
response = await generate_scam_response(user_message.content, conversations[session_id])
|
| 44 |
+
conversations[session_id].append({"role": "user", "content": user_message.content})
|
| 45 |
+
conversations[session_id].append({"role": "assistant", "content": response})
|
| 46 |
+
|
| 47 |
+
return {"scammer": response}
|
main_old.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import uuid
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from generate_message import generate_scam_message
|
| 6 |
+
from generate_response import generate_scam_response
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
conversation_histories = {}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Message(BaseModel):
|
| 23 |
+
session_id:str
|
| 24 |
+
content: str
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.get("/")
|
| 28 |
+
def root():
|
| 29 |
+
return {"messages:": "Hello World!"}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.post('/initial')
|
| 33 |
+
def get_response():
|
| 34 |
+
session_id = str(uuid.uuid4())
|
| 35 |
+
initial_scam_message = generate_scam_message()
|
| 36 |
+
conversation_histories[session_id] = [
|
| 37 |
+
{"role":"assistant", "content": initial_scam_message}
|
| 38 |
+
]
|
| 39 |
+
# conversation_history.append({"role": "assistant", "content":initial_scam_message})
|
| 40 |
+
return {"res": initial_scam_message}
|
| 41 |
+
|
| 42 |
+
@app.post('/chat')
|
| 43 |
+
def chat(user_message: Message):
|
| 44 |
+
session_id = user_message.session_id
|
| 45 |
+
|
| 46 |
+
if session_id not in conversation_histories:
|
| 47 |
+
raise HTTPException(status_code=400, detail="Session ID tidak ditemukan")
|
| 48 |
+
|
| 49 |
+
# Ambil riwayat percakapan untuk session ini
|
| 50 |
+
conversation_history = conversation_histories[session_id]
|
| 51 |
+
|
| 52 |
+
# Generate response dari scammer
|
| 53 |
+
response = generate_scam_response(user_message.content, conversation_history)
|
| 54 |
+
|
| 55 |
+
# Simpan pesan ke riwayat percakapan
|
| 56 |
+
conversation_history.append({"role": "user", "content": user_message.content})
|
| 57 |
+
conversation_history.append({"role": "assistant", "content": response})
|
| 58 |
+
|
| 59 |
+
return {"scammer": response}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# response = generate_scam_response(user_message.content, conversation_history)
|
| 64 |
+
# conversation_history.append({"role": "user", "content": user_message.content})
|
| 65 |
+
# conversation_history.append({"role": "assistant", "content": response})
|
| 66 |
+
# return {"scammer": response}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# conversation_history = []
|
| 72 |
+
# print("Chatbot edukasi scam dimulai! Ketik 'exit' untuk keluar.\n")
|
| 73 |
+
|
| 74 |
+
# # Generate pesan scam awal
|
| 75 |
+
# initial_scam_message = generate_scam_message()
|
| 76 |
+
# print(f"Scammer: {initial_scam_message}")
|
| 77 |
+
# conversation_history.append({"role": "assistant", "content": initial_scam_message})
|
| 78 |
+
|
| 79 |
+
# while True:
|
| 80 |
+
# user_input = input("Anda: ")
|
| 81 |
+
# if user_input.lower() == 'exit':
|
| 82 |
+
# print("Chatbot ditutup.")
|
| 83 |
+
# break
|
| 84 |
+
|
| 85 |
+
# response = generate_scam_response(user_input, conversation_history)
|
| 86 |
+
# conversation_history.append({"role": "user", "content": user_input})
|
| 87 |
+
# conversation_history.append({"role": "assistant", "content": response})
|
| 88 |
+
# print(f"Scammer: {response}")
|
requirements (1).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
openai
|
| 4 |
+
dotenv
|
user_input.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class Message(BaseModel):
|
| 4 |
+
session_id: str
|
| 5 |
+
content: str
|