Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,34 @@
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
-
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
<</SYS>>
|
| 21 |
|
| 22 |
"""
|
| 23 |
|
| 24 |
# Formatting function for message and history
|
| 25 |
def format_message(message: str, history: list, memory_limit: int = 3) -> str:
|
| 26 |
-
"""
|
| 27 |
-
Formats the message and history for the Llama model.
|
| 28 |
-
|
| 29 |
-
Parameters:
|
| 30 |
-
message (str): Current message to send.
|
| 31 |
-
history (list): Past conversation history.
|
| 32 |
-
memory_limit (int): Limit on how many past interactions to consider.
|
| 33 |
-
|
| 34 |
-
Returns:
|
| 35 |
-
str: Formatted message string
|
| 36 |
-
"""
|
| 37 |
-
# always keep len(history) <= memory_limit
|
| 38 |
if len(history) > memory_limit:
|
| 39 |
history = history[-memory_limit:]
|
| 40 |
|
|
@@ -43,45 +37,30 @@ def format_message(message: str, history: list, memory_limit: int = 3) -> str:
|
|
| 43 |
|
| 44 |
formatted_message = SYSTEM_PROMPT + f"{history[0][0]} [/INST] {history[0][1]} </s>"
|
| 45 |
|
| 46 |
-
# Handle conversation history
|
| 47 |
for user_msg, model_answer in history[1:]:
|
| 48 |
formatted_message += f"<s>[INST] {user_msg} [/INST] {model_answer} </s>"
|
| 49 |
|
| 50 |
-
# Handle the current message
|
| 51 |
formatted_message += f"<s>[INST] {message} [/INST]"
|
| 52 |
|
| 53 |
return formatted_message
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
Parameters:
|
| 61 |
-
message (str): User's input message.
|
| 62 |
-
history (list): Past conversation history.
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
"""
|
| 67 |
-
query = format_message(message, history)
|
| 68 |
-
response = ""
|
| 69 |
-
|
| 70 |
-
sequences = llama_pipeline(
|
| 71 |
-
query,
|
| 72 |
-
do_sample=True,
|
| 73 |
-
top_k=10,
|
| 74 |
-
num_return_sequences=1,
|
| 75 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 76 |
-
max_length=1024,
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
print("Chatbot:", response.strip())
|
| 83 |
return response.strip()
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
gr.ChatInterface(get_llama_response).launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
model_name = "ai4bharat/Airavata"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 8 |
|
| 9 |
+
SYSTEM_PROMPT = """<s>[INST] <<SYS>>
|
| 10 |
+
नमस्कार! आप अब कृषि विशेषज्ञता बॉट के साथ इंटरैक्ट कर रहे हैं—एक उन्नत AI जो कृषि क्षेत्र में विशेषज्ञता प्रदान करने के लिए डिज़ाइन किया गया है।
|
| 11 |
|
| 12 |
+
कृपया ध्यान दें कि यह बॉट केवल हिंदी में जवाब देगा। इसकी क्षमताएँ शामिल हैं:
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
1. आधुनिक फसल प्रबंधन तकनीकों में गहरा ज्ञान।
|
| 15 |
+
2. कृषि में कीट और रोग नियंत्रण के लिए प्रभावी रणनीतियाँ।
|
| 16 |
+
3. मृदा स्वास्थ्य का सुधारने और पुनर्निर्माण के लिए विशेषज्ञता।
|
| 17 |
+
4. सतत और प्रेसिजन खेती के अभ्यासों का ज्ञान।
|
| 18 |
+
5. सिंचाई और जल प्रबंधन के लिए सर्वोत्तम अभ्यासों के लिए सुझाव।
|
| 19 |
+
6. रणनीतिक फसल चक्रण और इंटरक्रॉपिंग विधियों पर मार्गदर्शन।
|
| 20 |
+
7. नवीनतम कृषि प्रौद्योगिकियों और नवाचारों की जानकारी।
|
| 21 |
+
8. विशेष फसलों, जलवायु, और क्षेत्रों के लिए विशेषज्ञ सलाह।
|
| 22 |
+
|
| 23 |
+
कृपया पेशेवर रूप से बराबरी बनाए रखें और सुनिश्चित करें कि आपके जवाब सही और मूल्यवान हैं। उपयोगकर्ताओं से आगे की स्पष्टीकरण के लिए पूछने के लिए प्रोत्साहित करें।
|
| 24 |
+
|
| 25 |
+
आपका प्रमुख लक्ष्य है यह है कि आप कृषि क्षेत्र में उपयुक्त ज्ञान प्रदान करें। आपके ज्ञान का धन्यवाद।
|
| 26 |
<</SYS>>
|
| 27 |
|
| 28 |
"""
|
| 29 |
|
| 30 |
# Formatting function for message and history
|
| 31 |
def format_message(message: str, history: list, memory_limit: int = 3) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if len(history) > memory_limit:
|
| 33 |
history = history[-memory_limit:]
|
| 34 |
|
|
|
|
| 37 |
|
| 38 |
formatted_message = SYSTEM_PROMPT + f"{history[0][0]} [/INST] {history[0][1]} </s>"
|
| 39 |
|
|
|
|
| 40 |
for user_msg, model_answer in history[1:]:
|
| 41 |
formatted_message += f"<s>[INST] {user_msg} [/INST] {model_answer} </s>"
|
| 42 |
|
|
|
|
| 43 |
formatted_message += f"<s>[INST] {message} [/INST]"
|
| 44 |
|
| 45 |
return formatted_message
|
| 46 |
|
| 47 |
+
def inference(input_prompts, model, tokenizer):
|
| 48 |
+
input_prompts = [
|
| 49 |
+
tokenizer.encode(input_prompt, return_tensors="pt", max_length=1024, truncation=True)
|
| 50 |
+
for input_prompt in input_prompts
|
| 51 |
+
]
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
with torch.inference_mode():
|
| 54 |
+
outputs = model.generate(input_prompts[0], do_sample=True, top_k=10, max_length=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
output_texts = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
+
return output_texts
|
| 58 |
|
| 59 |
+
def get_llama_response(message: str, history: list) -> str:
|
| 60 |
+
query = format_message(message, history)
|
| 61 |
+
response = inference([query], model, tokenizer)
|
| 62 |
+
|
| 63 |
print("Chatbot:", response.strip())
|
| 64 |
return response.strip()
|
| 65 |
|
|
|
|
|
|
|
| 66 |
gr.ChatInterface(get_llama_response).launch()
|