Spaces:
Running
Running
Commit
·
45c31be
1
Parent(s):
308ab4b
added gradio for the model_card punjabi_gemma
Browse files- app.py +48 -58
- requirements.txt +4 -1
app.py
CHANGED
@@ -1,64 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
history
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load your model and tokenizer
|
6 |
+
model_name = "modelsmafia/punjabi_Gemma-2B" # Replace with your model name
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
9 |
+
|
10 |
+
# Create a chat function
|
11 |
+
def chat_with_model(message, history):
|
12 |
+
# Format the conversation history for your model
|
13 |
+
# This format may need adjustment based on your model's expected input format
|
14 |
+
prompt = ""
|
15 |
+
for h in history:
|
16 |
+
prompt += f"<start_of_turn>user\n{h[0]}<end_of_turn>\n"
|
17 |
+
prompt += f"<start_of_turn>model\n{h[1]}<end_of_turn>\n"
|
18 |
+
|
19 |
+
prompt += f"<start_of_turn>user\n{message}<end_of_turn>\n<start_of_turn>model\n"
|
20 |
+
|
21 |
+
# Generate response
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
23 |
+
outputs = model.generate(
|
24 |
+
inputs.input_ids,
|
25 |
+
max_new_tokens=512,
|
26 |
+
temperature=0.7,
|
27 |
+
top_p=0.9,
|
28 |
+
do_sample=True,
|
29 |
+
pad_token_id=tokenizer.eos_token_id
|
30 |
+
)
|
31 |
+
|
32 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
33 |
+
|
34 |
+
# Remove any trailing "<end_of_turn>" if present
|
35 |
+
if "<end_of_turn>" in response:
|
36 |
+
response = response.split("<end_of_turn>")[0]
|
37 |
+
|
38 |
+
return response
|
39 |
+
|
40 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
41 |
demo = gr.ChatInterface(
|
42 |
+
chat_with_model,
|
43 |
+
title="Chat with Punjabi Gemma 2B",
|
44 |
+
description="A bilingual chat model for English and Punjabi",
|
45 |
+
examples=[
|
46 |
+
["ਸਤ ਸ੍ਰੀ ਅਕਾਲ, ਤੁਸੀਂ ਕਿਵੇਂ ਹੋ?"],
|
47 |
+
["Tell me about Punjab in a few sentences."]
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
],
|
49 |
+
theme="soft"
|
50 |
)
|
51 |
|
52 |
+
# Launch the interface
|
53 |
if __name__ == "__main__":
|
54 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
gradio
|
3 |
+
transformers
|
4 |
+
torch
|