Spaces:
Running
on
Zero
Running
on
Zero
import os | |
from collections.abc import Iterator | |
from threading import Thread | |
import gradio as gr | |
import spaces | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer | |
DESCRIPTION = """\ | |
# SILMA Kashif 2B Instruct v1.0 Playground | |
This is a demo of [`silma-ai/SILMA-Kashif-2B-Instruct-v1.0`](https://huggingface.co/silma-ai/SILMA-Kashif-2B-Instruct-v1.0). | |
** NOTE: Kashif is a RAG-optimized model, it is only trained to answer questions based on context. | |
""" | |
MAX_MAX_NEW_TOKENS = 1048 | |
DEFAULT_MAX_NEW_TOKENS = 600 | |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "11000")) | |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | |
model_id = "silma-ai/SILMA-Kashif-2B-Instruct-v1.0" | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
model = AutoModelForCausalLM.from_pretrained( | |
model_id, | |
device_map="auto", | |
torch_dtype=torch.bfloat16, | |
) | |
model.config.sliding_window = 12288 | |
model.eval() | |
def generate( | |
message: str, | |
chat_history: list[dict], | |
max_new_tokens: int = 1024, | |
temperature: float = 0.6, | |
) -> Iterator[str]: | |
conversation = chat_history.copy() | |
conversation.append({"role": "user", "content": message}) | |
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt") | |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH: | |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:] | |
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.") | |
input_ids = input_ids.to(model.device) | |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True) | |
generate_kwargs = dict( | |
{"input_ids": input_ids}, | |
streamer=streamer, | |
max_new_tokens=max_new_tokens, | |
do_sample=True, | |
temperature=temperature, | |
) | |
t = Thread(target=model.generate, kwargs=generate_kwargs) | |
t.start() | |
outputs = [] | |
for text in streamer: | |
outputs.append(text) | |
yield "".join(outputs) | |
demo = gr.ChatInterface( | |
fn=generate, | |
additional_inputs=[ | |
gr.Slider( | |
label="Max new tokens", | |
minimum=1, | |
maximum=MAX_MAX_NEW_TOKENS, | |
step=1, | |
value=DEFAULT_MAX_NEW_TOKENS, | |
), | |
gr.Slider( | |
label="Temperature", | |
minimum=0.01, | |
maximum=4.0, | |
step=0.1, | |
value=0.01, | |
) | |
], | |
stop_btn=None, | |
examples=[ | |
["Hello"], | |
["""أجب على السؤال بناءً على السياق أدناه | |
السياق: تشمل الاتفاقيات رسوم حمل سنوية ثابت قدها 30 مليون جنيه إسترليني للقنوات نظراً لأن كلاً من مزوديها قادرين على تأمين دفعات إضافية إذا ما حققت هذه القنوات أهدافاً متعلقةً بالأداء. لا يوجد حالياً ما يشير إلى ما إذا كان الاتفاق الجديد يشمل محتوىً إضافياً كالفيديو عند الطلب والدقة العالية ، كذلك الذي سبق أن قدمته بي سكاي بي. وقد وافقت كل من بي سكاي بي و فيرجين ميديا على إنهاء الدعاوى القضائية بالمحكمة العليا ضد بعضهما بشأن معاليم الحمل التي تخص قنواتهما الأساسية. | |
السؤال: ماسم الشركة التي وافقت على إنهاء دعواها القضائية ضد بي سكاي بي بالمحكمة العليا؟ | |
الإجابة: | |
"""], | |
["""Answer the question based on the context below:\n | |
Context:\n | |
For other people named Joseph Aoun, see Joseph Aoun (disambiguation). | |
His Excellency | |
Joseph Aoun | |
جوزيف عون | |
Aoun in 2025 | |
14th President of Lebanon | |
Incumbent | |
Assumed office | |
9 January 2025 | |
Prime Minister Najib Mikati | |
Nawaf Salam | |
Preceded by Najib Mikati (acting) | |
Michel Aoun | |
14th Commander of the Lebanese Armed Forces | |
Incumbent | |
Assumed office | |
8 March 2017 | |
President | |
Michel Aoun | |
Najib Mikati (acting) | |
Himself | |
Preceded by Jean Kahwaji | |
Personal details | |
Born 10 January 1964 (age 61) | |
Sin el Fil, Mount Lebanon, Lebanon | |
Spouse Nehmat Nehmeh | |
Children 2 | |
Education Lebanese American University (BA) | |
Lebanese Army Military Acad. | |
Military service | |
Allegiance Lebanon | |
Branch Lebanese Army | |
Service years 1983–present | |
Rank General | |
Wars Lebanese Civil War | |
Syrian civil war spillover in Lebanon | |
Joseph Khalil Aoun (/aʊn/; Arabic: جوزيف خليل عون; born 10 January 1964) is a Lebanese politician and general who became the 14th president of Lebanon on 9 January 2025.[1][2] He was appointed as the 14th Commander of the Lebanese Armed Forces in 2017. | |
Early life and education | |
Aoun was born on 10 January 1964, in the Beirut suburb of Sin el-Fil in the Metn District, the child of Hoda Ibrahim Makhlouta and Khalil Aoun.[3] He completed secondary school at the Collège des Frères Mont La Salle. His family is originally from the town of Al-Aaishiyah, Southern Lebanon. | |
Aoun enrolled at the Lebanese American University to pursue a bachelor's degree in political science and international affairs, which he earned in 2007. He also holds a bachelor's degree in military science from the Lebanese Army Military Academy.[4][5][6] | |
Military career | |
Aoun joined the Lebanese army in 1983. He trained abroad, especially in the United States and Syria. He also underwent counter-terrorism training in the United States in 2008 and Lebanon in 2013. He became head of the army's 9th Infantry Brigade in 2015. | |
Lebanese Civil War | |
In 1990, Aoun served as a lieutenant under the command of the Lebanese Army's Fawj al-Maghaweer (Arabic: فوج المغاوير, The Commando Regiment) under leader Bassam Gergi at the Adma barracks. During the Adma Battle in the Civil War's Liberation War's Elimination War, Gergi was killed and Aoun took over leadership within the Maghaweer group.[7][8] | |
Commander of Lebanese Armed Forces | |
In 2015, Aoun was appointed commander of the 9th Brigade deployed on the border with Israel. On 8 March 2017, the Lebanese government appointed him commander-in-chief of the Lebanese Armed Forces (LAF), replacing Jean Kahwaji.[9] | |
Aoun led battles against the Islamic State campaign in eastern Lebanon, where hundreds of Islamic State and Al-Nusra Front militants were entrenched on the border with Syria.[5] On 19 August 2017, he commanded the Jroud Dawn Operation, a successful offensive to expel the militants from their strongholds.[10] | |
Following protests in Lebanon and the political deadlock, General Aoun spoke out on 8 March 2021 criticising the Lebanese liquidity crisis and its impact on the military. His speech went viral on social media.[11] | |
On 15 December 2023, the Lebanese parliament voted to extend Aoun's term for one year, which was mainly endorsed by the Lebanese Opposition, the Amal Movement and the Progressive Socialist Party.[12] During this time, he led the LAF through the 2024 Israeli invasion of Lebanon. On 28 November 2024, parliament voted to extend his term a second time.[13] | |
\n | |
Question: is Joseph Aoun the president of Syria?\n | |
Answer: | |
"""] | |
], | |
cache_examples=False, | |
type="messages", | |
description=DESCRIPTION, | |
css_paths="style.css", | |
fill_height=True, | |
) | |
if __name__ == "__main__": | |
demo.queue(max_size=20).launch() |