Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,106 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
|
| 9 |
+
# Hugging Face API token
|
| 10 |
+
HF_API_TOKEN = "YOUR_HF_API_TOKEN"
|
| 11 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 12 |
|
| 13 |
+
# Folder to store per-user conversation histories
|
| 14 |
+
HISTORY_FOLDER = "histories"
|
| 15 |
+
os.makedirs(HISTORY_FOLDER, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# Available models
|
| 18 |
+
MODELS = {
|
| 19 |
+
"GPT-2": "gpt2",
|
| 20 |
+
"GPT-J": "EleutherAI/gpt-j-6B",
|
| 21 |
+
"GPT-NeoX": "EleutherAI/gpt-neox-20b"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def load_user_history(session_id):
|
| 25 |
+
path = os.path.join(HISTORY_FOLDER, f"{session_id}.json")
|
| 26 |
+
if os.path.exists(path):
|
| 27 |
+
with open(path, "r") as f:
|
| 28 |
+
return json.load(f)
|
| 29 |
+
return []
|
| 30 |
+
|
| 31 |
+
def save_user_history(session_id, history):
|
| 32 |
+
path = os.path.join(HISTORY_FOLDER, f"{session_id}.json")
|
| 33 |
+
with open(path, "w") as f:
|
| 34 |
+
json.dump(history, f, indent=2)
|
| 35 |
+
|
| 36 |
+
def ai_assistant(user_text, uploaded_file, model_name, session_id=None):
|
| 37 |
+
# Generate session_id if not provided
|
| 38 |
+
if session_id is None:
|
| 39 |
+
session_id = str(uuid.uuid4())
|
| 40 |
+
|
| 41 |
+
# Load this session's conversation
|
| 42 |
+
conversation_history = load_user_history(session_id)
|
| 43 |
+
|
| 44 |
+
# Combine file content if uploaded
|
| 45 |
+
if uploaded_file:
|
| 46 |
+
file_content = uploaded_file.read().decode("utf-8")
|
| 47 |
+
user_text = f"{user_text}\n\nFile content:\n{file_content}"
|
| 48 |
+
|
| 49 |
+
# Append user input
|
| 50 |
+
conversation_history.append(f"User: {user_text}")
|
| 51 |
+
|
| 52 |
+
# Prepare prompt with conversation history
|
| 53 |
+
prompt = "\n".join(conversation_history) + "\nAI:"
|
| 54 |
+
|
| 55 |
+
# Call Hugging Face Inference API
|
| 56 |
+
payload = {"inputs": prompt}
|
| 57 |
+
model_id = MODELS.get(model_name, "gpt2")
|
| 58 |
+
response = requests.post(
|
| 59 |
+
f"https://api-inference.huggingface.co/models/{model_id}",
|
| 60 |
+
headers=headers,
|
| 61 |
+
json=payload
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if response.status_code == 200:
|
| 65 |
+
result = response.json()
|
| 66 |
+
ai_text = result[0]["generated_text"] if isinstance(result, list) else str(result)
|
| 67 |
+
else:
|
| 68 |
+
ai_text = f"⚠️ Error: {response.status_code} - {response.text}"
|
| 69 |
+
|
| 70 |
+
# Append AI response
|
| 71 |
+
conversation_history.append(f"AI: {ai_text}")
|
| 72 |
+
|
| 73 |
+
# Save updated history for this session
|
| 74 |
+
save_user_history(session_id, conversation_history)
|
| 75 |
+
|
| 76 |
+
# Add timestamp and word count
|
| 77 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 78 |
+
word_count = len(ai_text.split())
|
| 79 |
+
display_text = f"{ai_text}\n\n🕒 Timestamp: {timestamp}"
|
| 80 |
+
|
| 81 |
+
return display_text, f"Word count: {word_count}", session_id
|
| 82 |
+
|
| 83 |
+
# Gradio Interface
|
| 84 |
+
demo = gr.Interface(
|
| 85 |
+
fn=ai_assistant,
|
| 86 |
+
inputs=[
|
| 87 |
+
gr.Textbox(label="Enter text", placeholder="Type your message here..."),
|
| 88 |
+
gr.File(label="Upload a text file (optional)"),
|
| 89 |
+
gr.Dropdown(label="Select AI Model", choices=list(MODELS.keys()), value="GPT-2"),
|
| 90 |
+
gr.Textbox(label="Session ID (auto-generated)", placeholder="Leave blank for new session", optional=True)
|
| 91 |
+
],
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Markdown(label="AI Response"),
|
| 94 |
+
gr.Textbox(label="Word Count"),
|
| 95 |
+
gr.Textbox(label="Session ID")
|
| 96 |
+
],
|
| 97 |
+
title="Session-Based Stateful GPT Assistant",
|
| 98 |
+
description=(
|
| 99 |
+
"Chat with an AI assistant with automatic session tracking. "
|
| 100 |
+
"Each session has its own conversation memory. Upload files, choose models, "
|
| 101 |
+
"and see timestamps & word counts. Session ID is returned for returning users."
|
| 102 |
+
)
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|