Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load model once (important for Spaces)
|
| 5 |
+
pipe = pipeline(
|
| 6 |
+
"text-generation",
|
| 7 |
+
model="ibm-granite/granite-3.3-2b-instruct",
|
| 8 |
+
device_map="auto"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Chat function
|
| 12 |
+
def generate_response(user_input):
|
| 13 |
+
messages = [
|
| 14 |
+
{"role": "user", "content": user_input},
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
output = pipe(messages, max_new_tokens=200)
|
| 18 |
+
|
| 19 |
+
# Handle output safely
|
| 20 |
+
try:
|
| 21 |
+
response = output[0]["generated_text"][-1]["content"]
|
| 22 |
+
except Exception:
|
| 23 |
+
response = str(output)
|
| 24 |
+
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
# Gradio UI
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=generate_response,
|
| 30 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask something..."),
|
| 31 |
+
outputs=gr.Textbox(label="Response"),
|
| 32 |
+
title="IBM Granite Chatbot",
|
| 33 |
+
description="Powered by granite-3.3-2b-instruct"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
interface.launch()
|