praveenpankaj commited on
Commit
c7b6715
1 Parent(s): dfc49a5

Updated app to aksara v1 dashboard

Browse files
Files changed (1) hide show
  1. app.py +103 -63
app.py CHANGED
@@ -1,66 +1,106 @@
1
- import os
2
  import gradio as gr
3
- import copy
 
4
  import time
5
- import llama_cpp
6
- from llama_cpp import Llama
7
- from huggingface_hub import hf_hub_download
8
-
9
- llm = Llama(
10
- model_path=hf_hub_download(
11
- repo_id="praveenpankaj/aksara_1_unsloth_q4",
12
- filename="aksara_-unsloth.Q4_K_M.gguf",
13
- ),
14
- n_ctx=1024,
15
- )
16
-
17
- history = []
18
-
19
- def generate_text(message, history):
20
- temp = ""
21
- input_prompt = "Ask akṣara anything on Agriculture in the Global South.\n"
22
- for interaction in history:
23
- input_prompt += "[|Umano|] " + interaction[0] + "\n"
24
- input_prompt += "[|Assistente|]" + interaction[1]
25
 
26
- input_prompt += "[|Umano|] " + message + "\n[|Assistente|]"
27
-
28
- print(input_prompt)
29
-
30
- output = llm(
31
- input_prompt,
32
- temperature=0.15,
33
- top_p=0.1,
34
- top_k=40,
35
- repeat_penalty=1.1,
36
- max_tokens=1024,
37
- stop=[
38
- "[|Umano|]",
39
- "[|Assistente|]",
40
- ],
41
- stream=True,
42
- )
43
- for out in output:
44
- stream = copy.deepcopy(out)
45
- temp += stream["choices"][0]["text"]
46
- yield temp
47
-
48
- history = ["init", input_prompt]
49
-
50
-
51
- demo = gr.ChatInterface(
52
- generate_text,
53
- title="akṣara running on CPU (quantized Q4_K)",
54
- description="This is a quantized version of akṣara running on CPU. It is a quantized version of the original version, that is running on a CPU machine.",
55
- examples=[
56
- "What are the recommended NPK dosage for maize varieties?",
57
- "Heavy rains are predicted next week. Is my rice crop ready for this, or should I harvest early?",
58
- "What crops can I grow during the dry season to use water more efficiently?"
59
- ],
60
- cache_examples=False,
61
- retry_btn=None,
62
- undo_btn="Delete Previous",
63
- clear_btn="Clear",
64
- )
65
- demo.queue(concurrency_count=1, max_size=5)
66
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import sqlite3
3
+ import pandas as pd
4
  import time
5
+ import huggingface_hub
6
+ import shutil
7
+ import os
8
+ import datetime
9
+ from apscheduler.schedulers.background import BackgroundScheduler
10
+
11
+ from rag_output import rag_response
12
+
13
+
14
+ TOKEN = os.environ.get('HFW_TOKEN')
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def llm_response(message, history):
17
+
18
+ res = rag_response(message)
19
+
20
+ for i in range(len(res)):
21
+ time.sleep(0.02)
22
+ yield res[: i+1]
23
+ # return res
24
+
25
+
26
+ def vote(response: gr.LikeData):
27
+ if response.liked:
28
+ add_review(1, response.value)
29
+ else:
30
+ add_review(0, response.value)
31
+
32
+
33
+ examples = ["What are the recommended NPK dosage for maize varieties?",
34
+ # "What are the recommended chemical treatments to control army worms in wheat crops?",
35
+ "Heavy rains are predicted next week. Is my rice crop ready for this, or should I harvest early?",
36
+ "What crops can I grow during the dry season to use water more efficiently?",
37
+ "How can I improve the health of my soil after a wheat harvest, using natural methods?",
38
+ # "Are there crop rotation techniques that can reduce fertilizer needs for barley?"
39
+ ]
40
+
41
+ # js_func = """
42
+ # function refresh() {
43
+ # const url = new URL(window.location);
44
+
45
+ # if (url.searchParams.get('__theme') !== 'light') {
46
+ # url.searchParams.set('__theme', 'light');
47
+ # window.location.href = url.href;
48
+ # }
49
+ # }
50
+ # """
51
+
52
+
53
+
54
+ description = "Hi! I am akṣara, an AI agronomist and I am here to help you with agriculture advisories for crops like paddy, wheat, maize, Sorghum, Barley, Cotton, Sugarcane, Soybean and Millets for the Indian subcontinent."
55
+
56
+ title = "akṣara"
57
+ theme = gr.themes.Soft(primary_hue="sky",)
58
+
59
+
60
+
61
+ chatbot = gr.Chatbot(height="450px",
62
+ show_copy_button=True,
63
+ show_label=False,
64
+ avatar_images=("user.webp","cropin.png"))
65
+
66
+ textbox = gr.Textbox(placeholder="Ask akṣara...",
67
+ min_width=300)
68
+ with gr.Blocks(theme=theme, title=title, css="footer {visibility: hidden}") as akshara:
69
+
70
+ gr.HTML("""<h1 style='font-family: sans-serif; text-align: center; font-size: 34px'>
71
+ <i style='color: #04A5D9' >akṣara</i> </h1>""")
72
+
73
+ gr.HTML("""<h3 style='font-family: sans-serif; text-align: left'>
74
+ Welcome! </h3>""")
75
+
76
+ # with gr.Column():
77
+
78
+ chatbot.like(vote, None, None)
79
+
80
+ gr.ChatInterface(fn=llm_response,
81
+ examples=examples,
82
+ cache_examples=False,
83
+ chatbot=chatbot,
84
+ description=description,
85
+ retry_btn="Retry",
86
+ undo_btn="Undo",
87
+ clear_btn="Clear",
88
+ submit_btn="Ask",
89
+ textbox=textbox
90
+ )
91
+
92
+ gr.HTML("""<h3 style='font-family: sans-serif; text-align: left'>
93
+ Disclaimer: Beta Test version #1.0 - akṣara is still in the beta testing stage and please verify information with agronomy experts or local extensions officers
94
+ """)
95
+
96
+
97
+
98
+ def display_ui():
99
+ akshara.launch()
100
+
101
+
102
+ if __name__ == "__main__":
103
+ display_ui()
104
+ pass
105
+
106
+