KarthikaRajagopal
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, render_template, request, jsonify,url_for
|
3 |
+
from werkzeug.utils import secure_filename
|
4 |
+
import os
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import openai
|
7 |
+
import requests
|
8 |
+
from gtts import gTTS
|
9 |
+
import asyncio
|
10 |
+
import string
|
11 |
+
import random
|
12 |
+
|
13 |
+
|
14 |
+
#load the api keys from the the .env file
|
15 |
+
load_dotenv()
|
16 |
+
#
|
17 |
+
hugging_face = os.getenv('hugging_face')
|
18 |
+
open_ai_key = os.getenv('open_ai_key')
|
19 |
+
#
|
20 |
+
openai.api_key = open_ai_key
|
21 |
+
|
22 |
+
app = Flask(__name__)
|
23 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
24 |
+
app.config['ALLOWED_EXTENSIONS'] = {'webm'}
|
25 |
+
|
26 |
+
|
27 |
+
def get_anwer_openai(quastion):
|
28 |
+
completion = openai.ChatCompletion.create(
|
29 |
+
model="gpt-3.5-turbo",
|
30 |
+
messages = [{"role": "system", "content" : "I want you to act like a helpful agriculture chatbot and help farmers with their query"},
|
31 |
+
{"role": "user", "content" : "Give a Brif Of Agriculture Seasons in India"},
|
32 |
+
{"role":"system","content":"In India, the agricultural season consists of three major seasons: the Kharif (monsoon), the Rabi (winter), and the Zaid (summer) seasons. Each season has its own specific crops and farming practices.\n\n1. Kharif Season (Monsoon Season):\nThe Kharif season typically starts in June and lasts until September. This season is characterized by the onset of the monsoon rains, which are crucial for agricultural activities in several parts of the country. Major crops grown during this season include rice, maize, jowar (sorghum), bajra (pearl millet), cotton, groundnut, turmeric, and sugarcane. These crops thrive in the rainy conditions and are often referred to as rain-fed crops.\n\n2. Rabi Season (Winter Season):\nThe Rabi season usually spans from October to March. This season is characterized by cooler temperatures and lesser or no rainfall. Crops grown during the Rabi season are generally sown in October and harvested in March-April. The major Rabi crops include wheat, barley, mustard, peas, gram (chickpeas), linseed, and coriander. These crops rely mostly on irrigation and are well-suited for the drier winter conditions.\n\n3. Zaid Season (Summer Season):\nThe Zaid season occurs between March and June and is a transitional period between Rabi and Kharif seasons. This season is marked by warmer temperatures and relatively less rainfall. The Zaid crops are grown during this time and include vegetables like cucumber, watermelon, muskmelon, bottle gourd, bitter gourd, and leafy greens such as spinach and amaranth. These crops are generally irrigated and have a shorter growing period compared to Kharif and Rabi crops.\n\nThese three agricultural seasons play a significant role in India's agricultural economy and provide stability to food production throughout the year. Farmers adapt their farming practices and crop selection accordingly to make the best use of the prevailing climatic conditions in each season."},
|
33 |
+
{"role":"user","content":quastion}
|
34 |
+
]
|
35 |
+
)
|
36 |
+
|
37 |
+
return completion['choices'][0]['message']['content']
|
38 |
+
|
39 |
+
|
40 |
+
###
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
def text_to_audio(text,filrname):
|
47 |
+
tts = gTTS(text)
|
48 |
+
tts.save(f'static/audio/{filrname}.mp3')
|
49 |
+
|
50 |
+
|
51 |
+
@app.route('/')
|
52 |
+
def index():
|
53 |
+
return render_template('index.html')
|
54 |
+
|
55 |
+
@app.route('/chat', methods=['POST'])
|
56 |
+
def chat():
|
57 |
+
if 'audio' in request.files:
|
58 |
+
audio = request.files['audio']
|
59 |
+
if audio and allowed_file(audio.filename):
|
60 |
+
filename = secure_filename(audio.filename)
|
61 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
62 |
+
audio.save(filepath)
|
63 |
+
transcription = process_audio(filepath)
|
64 |
+
return jsonify({'text': transcription})
|
65 |
+
|
66 |
+
text = request.form.get('text')
|
67 |
+
if text:
|
68 |
+
response = process_text(text)
|
69 |
+
return {'text': response['text'],'voice': url_for('static', filename='audio/' + response['voice'])}
|
70 |
+
|
71 |
+
return jsonify({'text': 'Invalid request'})
|
72 |
+
|
73 |
+
def allowed_file(filename):
|
74 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
75 |
+
|
76 |
+
def process_audio(filepath):
|
77 |
+
# Placeholder function for processing audio (speech-to-text transcription)
|
78 |
+
# Replace this with your own implementation using libraries like SpeechRecognition or DeepSpeech
|
79 |
+
#return 'hello This is a placeholder transcription for audio'
|
80 |
+
API_URL = "https://api-inference.huggingface.co/models/jonatasgrosman/wav2vec2-large-xlsr-53-english"
|
81 |
+
headers = {"Authorization": hugging_face}
|
82 |
+
with open(filepath, "rb") as f:
|
83 |
+
data = f.read()
|
84 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
85 |
+
data = response.json()
|
86 |
+
return data['text']
|
87 |
+
|
88 |
+
|
89 |
+
def process_text(text):
|
90 |
+
# Placeholder function for processing user's text input
|
91 |
+
# Replace this with your own implementation
|
92 |
+
return_text = get_anwer_openai(text)
|
93 |
+
#asyncio.run(text_to_audio(return_text))
|
94 |
+
# generating random strings
|
95 |
+
res = ''.join(random.choices(string.ascii_uppercase +
|
96 |
+
string.digits, k=8))
|
97 |
+
text_to_audio(return_text,res)
|
98 |
+
return {"text":return_text,"voice": f"{res}.mp3"}
|
99 |
+
|
100 |
+
|
101 |
+
if __name__ == '__main__':
|
102 |
+
app.run(debug=True)
|
103 |
+
|