Spaces:
Sleeping
Sleeping
# app_pure_llm.py | |
import os | |
import re | |
import gradio as gr | |
import openai | |
from openai import OpenAI | |
from langchain.text_splitter import CharacterTextSplitter | |
from sentence_transformers import SentenceTransformer | |
DARTMOUTH_CHAT_API_KEY = os.getenv('DARTMOUTH_CHAT_API_KEY') | |
if DARTMOUTH_CHAT_API_KEY is None: | |
raise ValueError("DARTMOUTH_CHAT_API_KEY not set.") | |
MODEL = "openai.gpt-4o-2024-08-06" | |
client = OpenAI( | |
base_url="https://chat.dartmouth.edu/api", # Replace with your endpoint URL | |
api_key=DARTMOUTH_CHAT_API_KEY, # Replace with your API key, if required | |
) | |
# --- Load and Prepare Data --- | |
# (Even if not used by the pure LLM function, we load the file to maintain consistency.) | |
with open("gen_agents.txt", "r", encoding="utf-8") as f: | |
full_text = f.read() | |
text_splitter = CharacterTextSplitter(separator="\n\n", chunk_size=512, chunk_overlap=20) | |
docs = text_splitter.create_documents([full_text]) | |
# You might not need passages for the pure LLM output, but we'll load them for completeness. | |
passages = [doc.page_content for doc in docs] | |
# --- Provided Function for Pure LLM --- | |
def generate_plain_answer(query): | |
""" | |
Generate an answer using GPT-4 without additional context. | |
""" | |
messages = [ | |
{"role": "system", "content": "You are a knowledgeable teaching assistant."}, | |
{"role": "user", "content": f"Answer the question: {query}"} | |
] | |
response = client.chat.completions.create( | |
model=MODEL, | |
messages=messages, | |
) | |
answer = response.choices[0].message.content.strip() | |
return answer | |
# --- Gradio App Function --- | |
def get_pure_llm_output(query): | |
answer = generate_plain_answer(query) | |
return f"<div style='white-space: pre-wrap;'>{answer}</div>" | |
def clear_output(): | |
return "" | |
# --- Custom CSS --- | |
custom_css = """ | |
body { | |
background-color: #343541 !important; | |
color: #ECECEC !important; | |
margin: 0; | |
padding: 0; | |
font-family: 'Inter', sans-serif; | |
} | |
#container { | |
max-width: 900px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
label { | |
color: #ECECEC; | |
font-weight: 600; | |
} | |
textarea, input { | |
background-color: #40414F; | |
color: #ECECEC; | |
border: 1px solid #565869; | |
} | |
button { | |
background-color: #565869; | |
color: #ECECEC; | |
border: none; | |
font-weight: 600; | |
transition: background-color 0.2s ease; | |
} | |
button:hover { | |
background-color: #6e7283; | |
} | |
.output-box { | |
border: 1px solid #565869; | |
border-radius: 4px; | |
padding: 10px; | |
margin-top: 8px; | |
background-color: #40414F; | |
} | |
""" | |
# --- Build Gradio Interface --- | |
with gr.Blocks(css=custom_css) as demo: | |
with gr.Column(elem_id="container"): | |
gr.Markdown("## Anonymous Chatbot\n### Loaded Article: Generative Agents - Interactive Simulacra of Human Behavior (Park et al. 2023)\n [https://arxiv.org/pdf/2304.03442](https://arxiv.org/pdf/2304.03442)") | |
gr.Markdown("Enter any questions about the article above in the prompt!") | |
query_input = gr.Textbox(label="Query", placeholder="Enter your query here...", lines=1) | |
with gr.Column(): | |
submit_button = gr.Button("Submit") | |
clear_button = gr.Button("Clear") | |
output_box = gr.HTML(label="Output", elem_classes="output-box") | |
submit_button.click(fn=get_pure_llm_output, inputs=query_input, outputs=output_box) | |
clear_button.click(fn=clear_output, inputs=[], outputs=output_box) | |
demo.launch() | |