Abrar20 commited on
Commit
37f5518
Β·
verified Β·
1 Parent(s): a4dded0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ from PyPDF2 import PdfReader
4
+ import os
5
+
6
+ # Load the Gemma model and tokenizer
7
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-9b-it")
8
+ model = AutoModelForCausalLM.from_pretrained("google/gemma-2-9b-it")
9
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
10
+
11
+ # Helper functions for document processing
12
+ def get_pdf_text(pdf_file):
13
+ text = ""
14
+ pdf_reader = PdfReader(pdf_file)
15
+ for page in pdf_reader.pages:
16
+ text += page.extract_text()
17
+ return text
18
+
19
+ def load_farming_knowledge_base(pdf_path="ai-farming.pdf"):
20
+ if not os.path.exists(pdf_path):
21
+ raise FileNotFoundError(f"PDF document '{pdf_path}' not found.")
22
+ return get_pdf_text(pdf_path)
23
+
24
+ # Load knowledge base from the farming PDF
25
+ knowledge_base = load_farming_knowledge_base()
26
+
27
+ # Chatbot response generation
28
+ def chatbot_response(user_message):
29
+ # Check if the question relates to the knowledge base
30
+ if user_message.lower() in knowledge_base.lower():
31
+ context = "This information is extracted from the AI Farming Guide:"
32
+ input_text = f"{context}\n{user_message}\n"
33
+ else:
34
+ context = "Answer based on general farming knowledge:"
35
+ input_text = f"{context}\n{user_message}\n"
36
+
37
+ # Generate a response using the Gemma model
38
+ response = pipe(input_text, max_length=512, temperature=0.7, top_p=0.9)
39
+ return response[0]["generated_text"]
40
+
41
+ # Gradio UI
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("# 🌾 AI Agri Farmer Chat Bot 🌿")
44
+ gr.Markdown(
45
+ "Welcome to the AI Agri Farmer Chat Bot! πŸ€– Ask your farming-related questions, "
46
+ "such as crop management, soil health, fertilizers, or pest control. If your "
47
+ "question isn't found in the farming guide, the bot will answer based on general knowledge."
48
+ )
49
+
50
+ with gr.Row():
51
+ user_input = gr.Textbox(
52
+ label="πŸ’¬ Ask your farming question:",
53
+ placeholder="Example: 'What is the best fertilizer for wheat?'",
54
+ )
55
+ chatbot_output = gr.Textbox(label="πŸ€– Chat Bot Response:")
56
+
57
+ example_inputs = gr.Examples(
58
+ examples=[
59
+ "What is the best fertilizer for rice?",
60
+ "How much water does maize need weekly?",
61
+ "What crops grow well in clay soil?",
62
+ ],
63
+ inputs=user_input,
64
+ )
65
+
66
+ def respond(input_text):
67
+ return chatbot_response(input_text)
68
+
69
+ user_input.submit(respond, inputs=user_input, outputs=chatbot_output)
70
+
71
+ gr.Markdown("### πŸ“„ About the Knowledge Base")
72
+ gr.Markdown(
73
+ "The chatbot uses information from the AI Farming Guide (`ai-farming.pdf`) as its primary source. "
74
+ "For topics not covered, it falls back on general farming knowledge."
75
+ )
76
+
77
+ # Launch the Gradio app
78
+ demo.launch()