Spaces:
Sleeping
Sleeping
Deploy Gradio app with multiple files
Browse files- app.py +42 -0
- config.py +2 -0
- models.py +22 -0
- requirements.txt +4 -0
- utils.py +3 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from models import chat_with_model
|
| 3 |
+
from config import MODEL_NAME
|
| 4 |
+
|
| 5 |
+
def chat_response(message, history):
|
| 6 |
+
# Format history for the model
|
| 7 |
+
conversation = []
|
| 8 |
+
for user_msg, assistant_msg in history:
|
| 9 |
+
conversation.append({"role": "user", "content": user_msg})
|
| 10 |
+
if assistant_msg:
|
| 11 |
+
conversation.append({"role": "assistant", "content": assistant_msg})
|
| 12 |
+
conversation.append({"role": "user", "content": message})
|
| 13 |
+
|
| 14 |
+
response = chat_with_model(conversation)
|
| 15 |
+
return response
|
| 16 |
+
|
| 17 |
+
with gr.Blocks(title="LLM Chatbot") as demo:
|
| 18 |
+
gr.Markdown("# 🤖 LLM Chatbot")
|
| 19 |
+
gr.Markdown("*Powered by a Hugging Face model*")
|
| 20 |
+
gr.Markdown("[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
|
| 21 |
+
|
| 22 |
+
chatbot = gr.Chatbot(type="messages", height=400)
|
| 23 |
+
msg = gr.Textbox(placeholder="Type your message here...", label="Message")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
submit_btn = gr.Button("Send")
|
| 27 |
+
clear_btn = gr.ClearButton([msg, chatbot])
|
| 28 |
+
|
| 29 |
+
def respond(message, chat_history):
|
| 30 |
+
if not message.strip():
|
| 31 |
+
return "", chat_history
|
| 32 |
+
|
| 33 |
+
response = chat_response(message, chat_history)
|
| 34 |
+
chat_history.append({"role": "user", "content": message})
|
| 35 |
+
chat_history.append({"role": "assistant", "content": response})
|
| 36 |
+
return "", chat_history
|
| 37 |
+
|
| 38 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 39 |
+
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|
config.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Configuration for the chatbot
|
| 2 |
+
MODEL_NAME = "microsoft/DialoGPT-medium" # Change to your preferred Hugging Face chat model
|
models.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from utils import format_conversation
|
| 3 |
+
from config import MODEL_NAME
|
| 4 |
+
|
| 5 |
+
# Load the model pipeline once
|
| 6 |
+
chat_pipeline = None
|
| 7 |
+
|
| 8 |
+
def get_chat_pipeline():
|
| 9 |
+
global chat_pipeline
|
| 10 |
+
if chat_pipeline is None:
|
| 11 |
+
chat_pipeline = pipeline("conversational", model=MODEL_NAME)
|
| 12 |
+
return chat_pipeline
|
| 13 |
+
|
| 14 |
+
def chat_with_model(conversation):
|
| 15 |
+
"""Send conversation to the Hugging Face model and get response."""
|
| 16 |
+
try:
|
| 17 |
+
pipe = get_chat_pipeline()
|
| 18 |
+
formatted_input = format_conversation(conversation)
|
| 19 |
+
response = pipe(formatted_input)
|
| 20 |
+
return response[0]['generated_text']
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error: {str(e)}"
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.0.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
accelerate>=0.20.0
|
utils.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def format_conversation(conversation):
|
| 2 |
+
"""Format conversation for the model."""
|
| 3 |
+
return "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation])
|