Paula Ayman
commited on
Commit
·
ea09339
1
Parent(s):
d29d8b9
3rd
Browse files- app.py +26 -4
- requirements.txt +3 -0
app.py
CHANGED
|
@@ -1,7 +1,29 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Pick any model you want to test (small for free Spaces)
|
| 6 |
+
MODEL_ID = "microsoft/phi-2"
|
| 7 |
|
| 8 |
+
# Load tokenizer + model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
torch_dtype=torch.float32, # use float32 for CPU (no GPU)
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def chat_with_ai(user_input):
|
| 16 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 17 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
return response
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
fn=chat_with_ai,
|
| 23 |
+
inputs=gr.Textbox(label="Your Message"),
|
| 24 |
+
outputs=gr.Textbox(label="AI Response"),
|
| 25 |
+
title="Test AI Chat Model",
|
| 26 |
+
description="Type a message to chat with an LLM hosted on Hugging Face."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|