Spaces:
Running
Running
Minte
commited on
Commit
·
a9e6d42
1
Parent(s):
d3d9706
dialog
Browse files- app.py +86 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Initialize model and tokenizer
|
| 6 |
+
model = None
|
| 7 |
+
tokenizer = None
|
| 8 |
+
|
| 9 |
+
print("🚀 Initializing DialoGPT-medium model...")
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
print("📥 Loading DialoGPT-medium model...")
|
| 13 |
+
model_name = "microsoft/DialoGPT-medium"
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 16 |
+
print("✅ DialoGPT-medium model loaded successfully!")
|
| 17 |
+
|
| 18 |
+
# Add padding token if it doesn't exist
|
| 19 |
+
if tokenizer.pad_token is None:
|
| 20 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 21 |
+
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"❌ Failed to load DialoGPT-medium model: {e}")
|
| 24 |
+
model = None
|
| 25 |
+
tokenizer = None
|
| 26 |
+
|
| 27 |
+
def respond(message, chat_history):
|
| 28 |
+
"""Respond to user message using DialoGPT"""
|
| 29 |
+
if model is None or tokenizer is None:
|
| 30 |
+
return "Model not loaded. Please try again later."
|
| 31 |
+
|
| 32 |
+
# Build conversation history
|
| 33 |
+
conversation = ""
|
| 34 |
+
for turn in chat_history:
|
| 35 |
+
conversation += f"User: {turn[0]}\nBot: {turn[1]}\n"
|
| 36 |
+
|
| 37 |
+
conversation += f"User: {message}\nBot:"
|
| 38 |
+
|
| 39 |
+
# Encode and generate
|
| 40 |
+
inputs = tokenizer.encode(conversation, return_tensors='pt', max_length=1024, truncation=True)
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model.generate(
|
| 44 |
+
inputs,
|
| 45 |
+
max_length=len(inputs[0]) + 128,
|
| 46 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
top_k=50,
|
| 50 |
+
top_p=0.95,
|
| 51 |
+
repetition_penalty=1.2
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
+
response = response.split("Bot:")[-1].strip()
|
| 56 |
+
|
| 57 |
+
# Clean response
|
| 58 |
+
if "\nUser:" in response:
|
| 59 |
+
response = response.split("\nUser:")[0]
|
| 60 |
+
|
| 61 |
+
chat_history.append((message, response))
|
| 62 |
+
return "", chat_history
|
| 63 |
+
|
| 64 |
+
# Create the chat interface
|
| 65 |
+
demo = gr.ChatInterface(
|
| 66 |
+
fn=respond,
|
| 67 |
+
title="💬 GihonTech AI Conversation Assistant",
|
| 68 |
+
description="Chat with an AI powered by Microsoft's DialoGPT-medium model",
|
| 69 |
+
examples=[
|
| 70 |
+
"Hello! How are you today?",
|
| 71 |
+
"What can you help me with?",
|
| 72 |
+
"Tell me about artificial intelligence",
|
| 73 |
+
"What's your favorite programming language?",
|
| 74 |
+
],
|
| 75 |
+
cache_examples=False,
|
| 76 |
+
retry_btn=None,
|
| 77 |
+
undo_btn="↩️ Undo",
|
| 78 |
+
clear_btn="🗑️ Clear"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch(
|
| 83 |
+
server_name="0.0.0.0",
|
| 84 |
+
server_port=7860,
|
| 85 |
+
share=False
|
| 86 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
+
accelerate>=0.20.0
|