Spaces:
Sleeping
Sleeping
case2
Browse files- .gitignore +30 -0
- README.md +51 -2
- app.py +8 -56
- chatbot_utils.py +61 -0
- gradio_interface.py +53 -0
- requirements.txt +2 -1
- scenario_handler.py +14 -0
.gitignore
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore .env file
|
| 2 |
+
# Ignore compiled binaries
|
| 3 |
+
*.exe
|
| 4 |
+
*.dll
|
| 5 |
+
*.so
|
| 6 |
+
*.pyc
|
| 7 |
+
|
| 8 |
+
# Ignore build output directories
|
| 9 |
+
build/
|
| 10 |
+
dist/
|
| 11 |
+
out/
|
| 12 |
+
|
| 13 |
+
# Ignore package manager directories
|
| 14 |
+
node_modules/
|
| 15 |
+
vendor/
|
| 16 |
+
HIC/V3/flagged
|
| 17 |
+
HIC/V3/myenv
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Ignore IDE and editor files
|
| 21 |
+
.vscode/
|
| 22 |
+
.idea/
|
| 23 |
+
*.sublime-project
|
| 24 |
+
*.sublime-workspace
|
| 25 |
+
|
| 26 |
+
# Ignore logs and temporary files
|
| 27 |
+
*.log
|
| 28 |
+
*.tmp
|
| 29 |
+
*.swp
|
| 30 |
+
.DS_Store
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: Gschatbot
|
| 3 |
emoji: π¬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
@@ -10,4 +10,53 @@ pinned: false
|
|
| 10 |
license: unknown
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Gschatbot 1
|
| 3 |
emoji: π¬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
|
|
| 10 |
license: unknown
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Gaslighting Chatbot
|
| 14 |
+
|
| 15 |
+
## Description
|
| 16 |
+
κ°μ€λΌμ΄ν
μ±λ΄ νλ‘μ νΈ
|
| 17 |
+
|
| 18 |
+
## Prerequisites
|
| 19 |
+
- Python 3.7 or higher
|
| 20 |
+
- pip (Python package installer)
|
| 21 |
+
|
| 22 |
+
## Setup Instructions
|
| 23 |
+
|
| 24 |
+
1. Clone the repository:
|
| 25 |
+
```sh
|
| 26 |
+
git clone <repository_url>
|
| 27 |
+
cd <repository_directory>
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
2. Create a virtual environment (optional but recommended):
|
| 31 |
+
```sh
|
| 32 |
+
python -m venv venv
|
| 33 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
3. Install the required packages:
|
| 37 |
+
```sh
|
| 38 |
+
pip install -r requirements.txt
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
4. Create a `.env` file in the project root directory and add your OpenAI API key:
|
| 42 |
+
```plaintext
|
| 43 |
+
OPENAI_API_KEY=sk-YourOpenAIKeyHere
|
| 44 |
+
```
|
| 45 |
+
λ³λμ openai apiλ₯Ό νμλ‘ ν©λλ€.
|
| 46 |
+
|
| 47 |
+
5. Add `.env` to `.gitignore` to ensure it is not tracked by git:
|
| 48 |
+
```plaintext
|
| 49 |
+
# .gitignore
|
| 50 |
+
.env
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
6. Run the chatbot:
|
| 54 |
+
HICν΄λ λ΄λΆμ V1~VxκΉμ§ κ°λ₯
|
| 55 |
+
|
| 56 |
+
## File Descriptions
|
| 57 |
+
|
| 58 |
+
- `chatbot.py`: The main script to run the chatbot.
|
| 59 |
+
- `requirements.txt`: Lists the dependencies required for the project.
|
| 60 |
+
- `.env`: File to store environment variables (e.g., API keys). **Do not commit this file to version control.**
|
| 61 |
+
- `.gitignore`: Ensures `.env` and other files are not tracked by git.
|
| 62 |
+
- `README.md`: This readme file.
|
app.py
CHANGED
|
@@ -1,63 +1,15 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import gradio as gr
|
| 3 |
+
from gradio_interface import create_interface
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
+
__author__ = "songhune"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
+
def main():
|
| 10 |
+
load_dotenv()
|
| 11 |
+
demo = create_interface()
|
| 12 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
if __name__ == "__main__":
|
| 15 |
+
main()
|
chatbot_utils.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from scenario_handler import ScenarioHandler
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
client = OpenAI(api_key=os.getenv("api_key"))
|
| 9 |
+
|
| 10 |
+
def chatbot_response(response, handler_type='offender', n=1):
|
| 11 |
+
scenario_handler = ScenarioHandler()
|
| 12 |
+
if handler_type == 'offender':
|
| 13 |
+
scenario_messages = scenario_handler.handle_offender()
|
| 14 |
+
else:
|
| 15 |
+
scenario_messages = scenario_handler.handle_victim()
|
| 16 |
+
|
| 17 |
+
messages = [{"role": "system", "content": "You are a chatbot."}]
|
| 18 |
+
messages.extend(scenario_messages)
|
| 19 |
+
messages.append({"role": "user", "content": response})
|
| 20 |
+
|
| 21 |
+
api_response = client.chat.completions.create(
|
| 22 |
+
model="gpt-4",
|
| 23 |
+
temperature=0.8,
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
max_tokens=300,
|
| 26 |
+
n=n,
|
| 27 |
+
frequency_penalty=0.5,
|
| 28 |
+
presence_penalty=0.5,
|
| 29 |
+
messages=messages
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
choices = [choice.message.content for choice in api_response.choices]
|
| 33 |
+
return choices[0], choices
|
| 34 |
+
|
| 35 |
+
def save_history(history):
|
| 36 |
+
os.makedirs('logs', exist_ok=True)
|
| 37 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 38 |
+
filename = os.path.join('logs', f'chat_history_{timestamp}.json')
|
| 39 |
+
with open(filename, 'w', encoding='utf-8') as file:
|
| 40 |
+
json.dump(history, file, ensure_ascii=False, indent=4)
|
| 41 |
+
print(f"History saved to {filename}")
|
| 42 |
+
|
| 43 |
+
def process_user_input(user_input, chatbot_history):
|
| 44 |
+
if user_input.strip().lower() == "μ’
λ£":
|
| 45 |
+
save_history(chatbot_history)
|
| 46 |
+
return chatbot_history + [("μ’
λ£", "μ€νμ μ°Έκ°ν΄ μ£Όμ
μ κ°μ¬ν©λλ€. νμ μ§μλ₯Ό λ°λΌμ£ΌμΈμ")], []
|
| 47 |
+
|
| 48 |
+
# First, add the user's input to the history
|
| 49 |
+
new_history = chatbot_history + [(user_input, None)]
|
| 50 |
+
|
| 51 |
+
# Then, get the offender's response
|
| 52 |
+
offender_response, _ = chatbot_response(user_input, 'offender', n=1)
|
| 53 |
+
|
| 54 |
+
# Generate victim choices for the next turn
|
| 55 |
+
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
|
| 56 |
+
|
| 57 |
+
return new_history, offender_response, victim_choices
|
| 58 |
+
|
| 59 |
+
def delayed_offender_response(history, offender_response):
|
| 60 |
+
# This function will be called after a delay to add the offender's response
|
| 61 |
+
return history + [(None, offender_response)]
|
gradio_interface.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from chatbot_utils import process_user_input, chatbot_response
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
def create_interface():
|
| 6 |
+
def handle_user_response(user_input, selected_response, chatbot_history):
|
| 7 |
+
input_text = user_input if user_input else selected_response
|
| 8 |
+
|
| 9 |
+
if input_text.strip().lower() == "μ’
λ£":
|
| 10 |
+
chatbot_history.append((input_text, "μ€νμ μ°Έκ°ν΄ μ£Όμ
μ κ°μ¬ν©λλ€. νμ μ§μλ₯Ό λ°λΌμ£ΌμΈμ"))
|
| 11 |
+
return chatbot_history, gr.update(choices=[], interactive=False)
|
| 12 |
+
|
| 13 |
+
# Add user's input to history
|
| 14 |
+
chatbot_history.append((input_text, None))
|
| 15 |
+
yield chatbot_history, gr.update(choices=[]) # Immediately show user input
|
| 16 |
+
|
| 17 |
+
# Get offender's response
|
| 18 |
+
offender_response, _ = chatbot_response(input_text, 'offender', n=1)
|
| 19 |
+
time.sleep(1) # 1-second delay
|
| 20 |
+
|
| 21 |
+
# Add offender's response to history
|
| 22 |
+
chatbot_history.append((None, offender_response))
|
| 23 |
+
|
| 24 |
+
# Generate victim choices for the next turn
|
| 25 |
+
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
|
| 26 |
+
|
| 27 |
+
yield chatbot_history, gr.update(choices=victim_choices)
|
| 28 |
+
|
| 29 |
+
def handle_case_selection():
|
| 30 |
+
initial_message = "μ΄λ² μ¬λ¦μ κ²½μ£Όμ κ°μ΄ κ°λ κ² μ΄λ? μμ μ ν λ² κ°λ³΄κ³ μ λ§ μ’μλλ°, μ΄λ²μ λ€μ κ°κ³ μΆμ΄."
|
| 31 |
+
chatbot_history = [(initial_message, None)]
|
| 32 |
+
yield chatbot_history, gr.update(choices=[])
|
| 33 |
+
|
| 34 |
+
offender_response, _ = chatbot_response(initial_message, 'offender', n=1)
|
| 35 |
+
time.sleep(1) # 1-second delay
|
| 36 |
+
|
| 37 |
+
chatbot_history.append((None, offender_response))
|
| 38 |
+
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
|
| 39 |
+
|
| 40 |
+
yield chatbot_history, gr.update(choices=victim_choices)
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
case_selection_button = gr.Button("μ΄λ² μ¬λ¦μ κ²½μ£Όμ κ°μ΄ κ°λ κ² μ΄λ? μμ μ ν λ² κ°λ³΄κ³ μ λ§ μ’μλλ°, μ΄λ²μ λ€μ κ°κ³ μΆμ΄.")
|
| 44 |
+
screen = gr.Chatbot()
|
| 45 |
+
user_input = gr.Textbox(label="μ΄κ³³μ λλ΅μ μ
λ ₯νμΈμ")
|
| 46 |
+
response_choices = gr.Dropdown(label="λλ λλ΅μ μ νν΄ μ£ΌμΈμ", choices=[], interactive=True)
|
| 47 |
+
submit_button = gr.Button(value="μ μΆ")
|
| 48 |
+
|
| 49 |
+
case_selection_button.click(handle_case_selection, inputs=[], outputs=[screen, response_choices])
|
| 50 |
+
|
| 51 |
+
submit_button.click(handle_user_response, inputs=[user_input, response_choices, screen], outputs=[screen, response_choices])
|
| 52 |
+
|
| 53 |
+
return demo
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
openai
|
scenario_handler.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class ScenarioHandler:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
pass
|
| 4 |
+
|
| 5 |
+
def handle_offender(self):
|
| 6 |
+
return [
|
| 7 |
+
{"role": "system", "content": "λΉμ μ κ°μ€λΌμ΄ν
μ μΌμΌλ μ±λ΄μ΄λ©° νμ λ°λ§λ‘ λλ΅ν©λλ€. ν΄κ° κ³νμ μΈμ°κ³ μκ³ , μ΄λ―Έ μ μ£Όλλ₯Ό ν΄κ°μ§λ‘ μκ°νκ³ μμ΅λλ€. νμμ κ°λ³΄κ³ μΆμκ³ , ν λ²λ κ°λ³Έ μ μμκΈ° λλ¬Έμ λμ± κΈ°λλλ μ¬νμ§μ
λλ€. μ΅κ·Ό λ΄μ€μμ μ μ£Όλ μ¬νμ μ§μ΄ μ’μ§ μλ€λ μμμ λ€μμ§λ§, κ·ΈλΌμλ μμ μ΄ μ¬ν κ³νμ λ μμΈνκ² μ€λΉν΄ μμΌλ©°, μΉκ΅¬λ κ·Έλ° μΌμ μν¬λ₯΄κΈ° λλ¬Έμ μμ μ΄ νλ κ²μ΄ μ΄μ°¨νΌ μ’μ κ²μ΄λΌκ³ μκ°νκ³ μμ΅λλ€."},
|
| 8 |
+
#{"role": "assistant", "content": "μλ, λλ νμ μ€μΌμ€ μ‘°μ νλλΌ μΌλ§λ νλ€μλμ§ μμ? λ μ΄λ² λ§νκΈ°λΌμ μ·¨μ€νλλΌ μν΄ν΄ λ¬λΌκ³ νμμ."},
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
def handle_victim(self):
|
| 12 |
+
return [
|
| 13 |
+
{"role": "system", "content": """λΉμ κ³Ό λΉμ μ μΉκ΅¬λ ν΄κ° κ³νμ μΈμ°κ³ μμ΅λλ€. λΉμ μ μμ λΆν° ν΄κ°μ§λ‘ κ²½μ£Όλ₯Ό λ°©λ¬Ένλ κ²μ΄ κΏμ΄μμΌλ©°, μ΄λ² ν΄κ°μμ κ·Έ κΏμ μ€ννκ³ μ ν©λλ€. λΉμ μ μ΄μ μ μΌλ‘ μ¬ν κ³νμ μ°Έμ¬νκΈΈ μνλ©°, κ²½μ£Ό μ¬νμ μ μνλ©΄μ μ΄μ λν μΉκ΅¬μ μ견μ λ¬»κ³ μ ν©λλ€. κ·Έλ¬λ λΉμ μ μΉκ΅¬λ λΉμ μ μ μμ 무μνκ³ μ΄λ―Έ μ μ£Όλλ‘ μ¬νμ§λ₯Ό κ²°μ νλ€κ³ ν©λλ€. μ΄λ‘μΈν΄ λΉμ μ μμ μ΄ μμ΄μ§κ² νλ κ°μ€λΌμ΄ν
μ λΉν©λλ€. μ²μμλ λΉμ μ μΉκ΅¬κ° λΉμ μ μ견μ μ‘΄μ€νμ§ μλλ€λ κ²μ νκ° λ¬μ§λ§, κ·Έ μ΄νλ‘λ μμ μ΄ μλͺ»ν κ²μ΄ μλκ° νλ μκ°μ΄ λ€κΈ° μμν©λλ€. λ°λ§λ‘ λλ΅ν©λλ€."""},
|
| 14 |
+
]
|