Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
# Load the model pipeline
|
| 7 |
+
pipe = pipeline("text-generation", model="google/vaultgemma-1b", device="cuda", torch_dtype=torch.float16)
|
| 8 |
+
|
| 9 |
+
# Define the chat function
|
| 10 |
+
@spaces.GPU(duration=120)
|
| 11 |
+
def chat(message, history):
|
| 12 |
+
# Format the conversation history for the model
|
| 13 |
+
prompt = ""
|
| 14 |
+
for user_msg, bot_msg in history:
|
| 15 |
+
prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 16 |
+
prompt += f"User: {message}\nAssistant:"
|
| 17 |
+
|
| 18 |
+
# Generate response
|
| 19 |
+
response = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_p=0.9)
|
| 20 |
+
generated_text = response[0]['generated_text']
|
| 21 |
+
|
| 22 |
+
# Extract only the assistant's response
|
| 23 |
+
assistant_response = generated_text.split("Assistant:")[-1].strip()
|
| 24 |
+
|
| 25 |
+
return assistant_response
|
| 26 |
+
|
| 27 |
+
# Create the Gradio chat interface
|
| 28 |
+
demo = gr.ChatInterface(
|
| 29 |
+
fn=chat,
|
| 30 |
+
title="VaultGemma-1B Chatbot",
|
| 31 |
+
description="A chatbot powered by Google's VaultGemma-1B model.",
|
| 32 |
+
theme="soft",
|
| 33 |
+
examples=[
|
| 34 |
+
"What is the capital of France?",
|
| 35 |
+
"Tell me a joke.",
|
| 36 |
+
"Explain quantum computing in simple terms."
|
| 37 |
+
],
|
| 38 |
+
concurrency_limit=1
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the app
|
| 42 |
+
demo.launch()
|