Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
def respond(message, history):
|
| 5 |
+
"""Process user message with conversation history"""
|
| 6 |
+
# Format conversation history
|
| 7 |
+
prompt = ""
|
| 8 |
+
if history:
|
| 9 |
+
for human_msg, bot_msg in history:
|
| 10 |
+
prompt += f"Human: {human_msg}\nAssistant: {bot_msg}\n"
|
| 11 |
+
|
| 12 |
+
# Add current message
|
| 13 |
+
prompt += f"Human: {message}\nAssistant:"
|
| 14 |
+
|
| 15 |
+
# Initialize chatbot
|
| 16 |
+
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 17 |
+
|
| 18 |
+
# Generate response
|
| 19 |
+
response = chatbot(
|
| 20 |
+
prompt,
|
| 21 |
+
max_length=200,
|
| 22 |
+
temperature=0.7,
|
| 23 |
+
top_p=0.9,
|
| 24 |
+
repetition_penalty=1.1,
|
| 25 |
+
do_sample=True,
|
| 26 |
+
pad_token_id=50256
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Extract only the assistant's response
|
| 30 |
+
full_text = response[0]['generated_text']
|
| 31 |
+
assistant_response = full_text.split("Assistant:")[-1].strip()
|
| 32 |
+
|
| 33 |
+
return assistant_response
|
| 34 |
+
|
| 35 |
+
# Create Gradio interface
|
| 36 |
+
demo = gr.ChatInterface(
|
| 37 |
+
fn=respond,
|
| 38 |
+
title="?? SimpleAI Chatbot",
|
| 39 |
+
description="A conversational AI powered by DialoGPT",
|
| 40 |
+
examples=["Hello!", "What's AI?", "Tell me a joke"],
|
| 41 |
+
theme="soft",
|
| 42 |
+
retry_btn=None,
|
| 43 |
+
undo_btn=None,
|
| 44 |
+
clear_btn="Clear Chat"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|