Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_core.documents import Document
|
| 7 |
+
|
| 8 |
+
# ==========================================
|
| 9 |
+
# 1. INITIALIZE MODELS (CPU OPTIMIZED)
|
| 10 |
+
# ==========================================
|
| 11 |
+
print("Loading Embedding Model...")
|
| 12 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 13 |
+
|
| 14 |
+
print("Loading LLM (TinyLlama) on CPU...")
|
| 15 |
+
# Using TinyLlama as it is small enough to run inference on a basic CPU without crashing
|
| 16 |
+
chat_pipeline = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 19 |
+
device="cpu",
|
| 20 |
+
torch_dtype=torch.float32,
|
| 21 |
+
max_new_tokens=256
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# ==========================================
|
| 25 |
+
# 2. BUILD FAISS KNOWLEDGE BASE
|
| 26 |
+
# ==========================================
|
| 27 |
+
# In a real app, you would load PDFs or URLs here. We are using mock news data.
|
| 28 |
+
mock_news_data = [
|
| 29 |
+
"AI taking over jobs: Studies show AI will displace 85 million jobs, but create 97 million new ones by 2025.",
|
| 30 |
+
"Remote work: Data indicates remote workers are 13% more productive but struggle with team collaboration.",
|
| 31 |
+
"Electric Vehicles: EVs produce zero tailpipe emissions, but battery mining has significant environmental impacts.",
|
| 32 |
+
"Universal Basic Income: UBI trials show decreased poverty rates but raise concerns about inflation and work disincentives."
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
print("Building FAISS Index...")
|
| 36 |
+
documents = [Document(page_content=text) for text in mock_news_data]
|
| 37 |
+
vector_store = FAISS.from_documents(documents, embedding_model)
|
| 38 |
+
|
| 39 |
+
# ==========================================
|
| 40 |
+
# 3. DEBATE AGENT LOGIC
|
| 41 |
+
# ==========================================
|
| 42 |
+
def debate_agent(user_message, history):
|
| 43 |
+
# 1. Retrieve relevant facts from FAISS based on the user's stance
|
| 44 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 1})
|
| 45 |
+
docs = retriever.invoke(user_message)
|
| 46 |
+
context = docs[0].page_content if docs else "No specific news context found."
|
| 47 |
+
|
| 48 |
+
# 2. Construct the Prompt
|
| 49 |
+
# We use TinyLlama's specific chat template format (<|system|>, <|user|>, <|assistant|>)
|
| 50 |
+
system_prompt = (
|
| 51 |
+
"You are an expert Debate Agent. The user will state a topic and their stance. "
|
| 52 |
+
"You must disagree with them, providing a polite counter-argument. "
|
| 53 |
+
"At the very end of your response, score the user's argument strength from 1 to 10 based on logic. "
|
| 54 |
+
f"Use this news fact to support your counter-argument: {context}"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
prompt = f"<|system|>\n{system_prompt}</s>\n<|user|>\n{user_message}</s>\n<|assistant|>\n"
|
| 58 |
+
|
| 59 |
+
# 3. Generate Response
|
| 60 |
+
try:
|
| 61 |
+
outputs = chat_pipeline(
|
| 62 |
+
prompt,
|
| 63 |
+
do_sample=True,
|
| 64 |
+
temperature=0.7,
|
| 65 |
+
top_k=50,
|
| 66 |
+
top_p=0.95
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Extract only the assistant's reply
|
| 70 |
+
generated_text = outputs[0]["generated_text"]
|
| 71 |
+
response = generated_text.split("<|assistant|>\n")[-1].strip()
|
| 72 |
+
return response
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"An error occurred during generation: {str(e)}"
|
| 76 |
+
|
| 77 |
+
# ==========================================
|
| 78 |
+
# 4. GRADIO USER INTERFACE
|
| 79 |
+
# ==========================================
|
| 80 |
+
# Designing a clean UI inspired by the reference images
|
| 81 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"""
|
| 84 |
+
# 🤖 AI News Debate & Summary Agent
|
| 85 |
+
**About This Agent:** Enter a topic and your stance (e.g., "I think remote work is bad for productivity").
|
| 86 |
+
The agent will retrieve relevant news facts, formulate a counter-argument, and score the strength of your initial point.
|
| 87 |
+
"""
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
chat_interface = gr.ChatInterface(
|
| 91 |
+
fn=debate_agent,
|
| 92 |
+
fill_height=True,
|
| 93 |
+
examples=[
|
| 94 |
+
"I believe AI will eventually destroy all human jobs.",
|
| 95 |
+
"Electric vehicles are the only way to save the environment.",
|
| 96 |
+
"Universal Basic Income will just make people lazy."
|
| 97 |
+
]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
# Launching with parameters suitable for Hugging Face Spaces
|
| 102 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|