S-Dreamer commited on
Commit
74950d2
·
verified ·
1 Parent(s): 815d957

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -66
app.py CHANGED
@@ -1,73 +1,98 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- """
4
- NSFW Text Descriptor using TF-IDF and Cosine Similarity
5
- Optimized for modularity, memory efficiency, and Gradio integration.
6
- """
7
-
8
  import gradio as gr
9
- import numpy as np
 
 
10
  from datasets import load_dataset
11
- from sklearn.feature_extraction.text import TfidfVectorizer
12
- from sklearn.metrics.pairwise import cosine_similarity
13
- from itertools import chain
14
- from typing import List
15
-
16
-
17
- class NSFWTextMatcher:
18
- def __init__(self):
19
- self.dataset_sources = [
20
- "aifeifei798/DPO_Pairs-Roleplay-NSFW",
21
- "Maxx0/sexting-nsfw-adultconten",
22
- "QuietImpostor/Claude-3-Opus-Claude-3.5-Sonnnet-9k",
23
- "HuggingFaceTB/everyday-conversations-llama3.1-2k",
24
- "Chadgpt-fam/sexting_dataset"
25
- ]
26
- self.all_texts = self._load_all_texts()
27
- self.vectorizer = TfidfVectorizer()
28
- self.tfidf_matrix = self.vectorizer.fit_transform(self.all_texts)
29
-
30
- def _load_all_texts(self) -> List[str]:
31
- texts = []
32
- for source in self.dataset_sources:
33
- try:
34
- dataset = load_dataset(source)
35
- for split in dataset:
36
- features = dataset[split].features
37
- if 'text' in features:
38
- texts.extend(dataset[split]['text'])
39
- elif 'content' in features:
40
- texts.extend(dataset[split]['content'])
41
- except Exception as e:
42
- print(f"[WARN] Failed to load dataset {source}: {e}")
43
- return texts
44
-
45
- def find_best_match(self, input_text: str) -> str:
46
- input_vector = self.vectorizer.transform([input_text])
47
- similarity_scores = cosine_similarity(input_vector, self.tfidf_matrix)
48
- best_match_idx = np.argmax(similarity_scores)
49
- return self.all_texts[best_match_idx]
50
-
51
-
52
- # Instantiate the matcher once (can be made lazy if needed)
53
- matcher = NSFWTextMatcher()
54
-
55
 
56
- def generate_text(input_text: str) -> str:
57
- if not input_text.strip():
58
- return "Please enter a valid input."
59
- return matcher.find_best_match(input_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
 
 
61
 
62
- # Gradio Interface
63
- iface = gr.Interface(
64
- fn=generate_text,
65
- inputs=gr.Textbox(label="Enter text to describe"),
66
- outputs=gr.Textbox(label="Best Match"),
67
- title="NSFW Text Descriptor",
68
- description="Match your input with the most similar description from NSFW datasets using TF-IDF.",
69
- allow_flagging="never",
70
- )
71
 
 
72
  if __name__ == "__main__":
73
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
 
 
 
 
 
 
2
  import gradio as gr
3
+ from agents.nsfw_agent import NSFWSemanticChatAgent
4
+ from generators.llm_backend import OpenAIBackend, HuggingFaceBackend
5
+ from prompts.nsfw_templates import NSFWPromptTemplate
6
  from datasets import load_dataset
7
+ import os
8
+ from typing import List, Tuple
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ class NSFWSemanticChatbot:
11
+ def __init__(self, backend_type: str = "openai"):
12
+ """Initialize the complete chatbot system"""
13
+ self.agent = NSFWSemanticChatAgent()
14
+ self.prompt_template = NSFWPromptTemplate()
15
+
16
+ # Initialize generation backend
17
+ if backend_type == "openai":
18
+ api_key = os.getenv("OPENAI_API_KEY")
19
+ if not api_key:
20
+ raise ValueError("OpenAI API key required")
21
+ self.generator = OpenAIBackend(api_key)
22
+ else:
23
+ self.generator = HuggingFaceBackend()
24
+
25
+ self._load_dataset()
26
+
27
+ def _load_dataset(self) -> None:
28
+ """Load and process NSFW dialogue dataset"""
29
+ try:
30
+ # Load your NSFW dataset here
31
+ # Example: dataset = load_dataset("your_nsfw_dataset")
32
+ # For demonstration, using placeholder data
33
+ sample_data = [
34
+ "That's such an interesting perspective...",
35
+ "I love how you think about these things...",
36
+ "Tell me more about what you're feeling...",
37
+ # Add your actual NSFW dialogue samples
38
+ ]
39
+ self.agent.build_index(sample_data)
40
+ except Exception as e:
41
+ print(f"Dataset loading error: {e}")
42
+
43
+ def generate_response(
44
+ self,
45
+ chat_history: List[Tuple[str, str]],
46
+ user_input: str
47
+ ) -> List[Tuple[str, str]]:
48
+ """Main response generation pipeline"""
49
+
50
+ if not user_input.strip():
51
+ return chat_history + [(user_input, "⚠️ Please provide input")]
52
+
53
+ try:
54
+ # Step 1: Retrieve semantically similar examples
55
+ retrieved_examples = self.agent.retrieve_context(user_input, k=3)
56
+
57
+ # Step 2: Build contextual prompt
58
+ prompt = self.prompt_template.build_context_prompt(
59
+ user_input, chat_history, retrieved_examples
60
+ )
61
+
62
+ # Step 3: Generate response
63
+ bot_response = self.generator.generate_response(prompt, max_tokens=150)
64
+
65
+ # Step 4: Update conversation history
66
+ updated_history = chat_history + [(user_input, bot_response)]
67
+
68
+ return updated_history
69
+
70
+ except Exception as e:
71
+ error_response = f"System error: {str(e)}"
72
+ return chat_history + [(user_input, error_response)]
73
 
74
+ # Initialize chatbot instance
75
+ chatbot = NSFWSemanticChatbot(backend_type="openai") # or "huggingface"
76
 
77
+ # Create Gradio interface
78
+ def chat_interface(message, history):
79
+ """Gradio-compatible chat interface"""
80
+ return chatbot.generate_response(history, message)
 
 
 
 
 
81
 
82
+ # Launch application
83
  if __name__ == "__main__":
84
+ demo = gr.ChatInterface(
85
+ fn=chat_interface,
86
+ title="🔞 NSFW Semantic Chatbot",
87
+ description="Advanced conversational AI using semantic embeddings and retrieval-augmented generation",
88
+ theme="soft",
89
+ retry_btn="Regenerate Response",
90
+ undo_btn="Undo Last",
91
+ clear_btn="Clear Conversation"
92
+ )
93
+
94
+ demo.launch(
95
+ server_name="0.0.0.0",
96
+ server_port=7860,
97
+ share=True
98
+ )