File size: 4,055 Bytes
86b946a
 
bfaeb19
 
 
a6532a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6d394e
 
a6532a3
 
 
 
 
 
 
 
 
 
 
a6d394e
 
 
 
a6532a3
 
a6d394e
a6532a3
 
 
 
2dfe1eb
1e3c563
2dfe1eb
 
 
a6d394e
2dfe1eb
a6532a3
 
 
 
cab6a89
 
 
 
a6532a3
 
 
 
 
 
cab6a89
b151e56
 
a6532a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d06f4eb
a29f440
a6d394e
a29f440
883b37e
f845a0a
1e3c563
a197dc7
883b37e
1e3c563
a6d394e
883b37e
e0d541d
3405778
a6d394e
1e3c563
cab6a89
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import streamlit as st
from gradio_client import Client
from gtts import gTTS
import os

# Constants
TITLE = "AgriTure"
DESCRIPTION = """
----
This Project demonstrates a model fine-tuned by Achyuth. This Model is named as "AgriTure". This Model helps the farmers and scientists to develop the art of agriculture and farming.
Hope this will be a Successful Project!!!
~Achyuth
----
"""

# Initialize client
with st.sidebar:
    system_promptSide = st.text_input("Optional system prompt:")
    temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
    max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
    ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
    RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)

whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")


def transcribe(wav_path):
    return whisper_client.predict(
        wav_path,
        "transcribe",
        api_name="/predict"
    )

# Prediction function
def predict(message, system_prompt='Your name is OpenGPT. You are developed by Achyuth. You need to mostly focus on giving information about future agriculture and advanced farming. Empower yourself farming future with cutting-edge technology and sustainable practices. You need to cultivate a greener and more productive. Your developer is studying in The Hyderabad Public School Kadapa.', temperature=0.7, max_new_tokens=4096, Topp=0.5, Repetitionpenalty=1.2):
    with st.status("Starting client"):
        client = Client("https://huggingface-projects-llama-2-7b-chat.hf.space/")
        st.write("Requesting Audio Transcriber")
    with st.status("Requesting AgriTure v1"):
        st.write("Requesting API")
        response = client.predict(
            message,
            system_prompt,
            max_new_tokens,
            temperature,
            Topp,
            500,
            Repetitionpenalty,
            api_name="/chat"
        )
        st.write("Done")
        return response

# Function to convert text to speech
def text_to_speech(text, language='en', filename='output.mp3'):
    tts = gTTS(text=text, lang=language, slow=False)
    tts.save(filename)
    os.system(f'start {filename}')

# Streamlit UI
st.title(TITLE)
st.write(DESCRIPTION)

# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦™')):
        st.markdown(message["content"])

textinput = st.chat_input("Ask AgriTure anything...")

# Assume st_audiorec is a simplified audio recorder function
wav_audio_data = st.audio_recorder()

if wav_audio_data is not None:
    with st.status("Transcribing audio..."):
        with open("audio.wav", "wb") as f:
            f.write(wav_audio_data)
        prompt = transcribe("audio.wav")
        st.write("Transcribed Given Audio βœ”")

    st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
    st.session_state.messages.append({"role": "human", "content": prompt})

    # transcribe audio
    response = predict(message=prompt)

    with st.chat_message("assistant", avatar='πŸ¦™'):
        st.markdown(response)
    st.session_state.messages.append({"role": "assistant", "content": response})

    # Convert response to audio
    text_to_speech(response)

# React to user input
if prompt := textinput:
    st.chat_message("human", avatar="πŸ’¬: ").markdown(prompt)
    st.session_state.messages.append({"role": "human", "content": prompt})

    response = predict(message=prompt)

    with st.chat_message("assistant", avatar='πŸ¦™'):
        st.markdown(response)
    st.session_state.messages.append({"role": "assistant", "content": response})

    # Convert response to audio
    text_to_speech(response)