Spaces:
No application file
No application file
Cool Shot Systems
Browse files- app (2).py +59 -0
- requirements (2).txt +4 -0
app (2).py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load a SMALL and FAST model
|
| 6 |
+
print("Loading AI model...")
|
| 7 |
+
model_name = "microsoft/DialoGPT-small" # Small = Fast!
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
print("Model loaded!")
|
| 11 |
+
|
| 12 |
+
# Store chat history for context
|
| 13 |
+
chat_history_ids = None
|
| 14 |
+
|
| 15 |
+
def chat(message, history):
|
| 16 |
+
"""
|
| 17 |
+
Fast AI chat using DialoGPT-small model.
|
| 18 |
+
"""
|
| 19 |
+
global chat_history_ids
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# Encode user input
|
| 23 |
+
new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
|
| 24 |
+
|
| 25 |
+
# Append to chat history or start fresh
|
| 26 |
+
if chat_history_ids is not None and len(history) > 0:
|
| 27 |
+
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
|
| 28 |
+
else:
|
| 29 |
+
bot_input_ids = new_input_ids
|
| 30 |
+
|
| 31 |
+
# Generate response (fast settings)
|
| 32 |
+
chat_history_ids = model.generate(
|
| 33 |
+
bot_input_ids,
|
| 34 |
+
max_length=200,
|
| 35 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
top_k=50,
|
| 38 |
+
temperature=0.7
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Decode response
|
| 42 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
+
return response if response.strip() else "Hmm, let me think... Could you say that differently?"
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
chat_history_ids = None # Reset on error
|
| 48 |
+
return f"Let me try again: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# Create Gradio Chat Interface
|
| 51 |
+
demo = gr.ChatInterface(
|
| 52 |
+
fn=chat,
|
| 53 |
+
title="🤖 AI Chat Assistant",
|
| 54 |
+
description="Fast AI Chat - Powered by DialoGPT",
|
| 55 |
+
examples=["Hello!", "Tell me a joke", "How are you?", "What's your name?"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|
requirements (2).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.36.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
accelerate>=0.25.0
|