Dooratre commited on
Commit
8d14bb9
·
verified ·
1 Parent(s): 6ca7122

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -6
app.py CHANGED
@@ -1,12 +1,163 @@
1
- from flask import Flask, render_template
2
-
 
 
 
3
  app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  @app.route('/')
6
- def display_info():
7
- with open('info.txt', 'r') as file:
8
- info = file.read().replace('\n', '<br>')
9
- return render_template('index.html', info=info)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  if __name__ == '__main__':
12
  app.run(host="0.0.0.0", port=7860)
 
1
+ import requests
2
+ from flask import Flask, render_template, request, send_from_directory
3
+ from datetime import datetime
4
+ from bs4 import BeautifulSoup
5
+ from googletrans import Translator
6
  app = Flask(__name__)
7
+ with open('i.txt', 'r') as file:
8
+ data = file.read()
9
+
10
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
11
+ headers = {"Authorization": f"Bearer hf{data}"}
12
+
13
+
14
+ def query(payload):
15
+ response = requests.post(API_URL, headers=headers, json=payload)
16
+ return response.json()
17
+
18
+ conversation_history = []
19
+
20
+ def generate_response(user_input):
21
+ bitcoin_price, current_time = get_bitcoin_price()
22
+ result = get_div_content(url)
23
+ new_query = {
24
+ "inputs": f"information about yourself: you are helpful assistant and your name is (Niron) , you are trained and proggramed by Mr.omar nuwara he is create you for btc trad and predicition start chat with user with use good Emojeis\n\nBtc Price Now :${bitcoin_price} date and time now: {current_time}\n\n Bitcoin history Tidy is (Date/open/high/low/close/adj close/Volume): {result}\n\nLast news:{news}\n\ntask:complete the reesponse:\n\nconversation history:{conversation_history}\n\nUser message:{user_input}\n\nmake sure to response about it and don't generate alot of words just based on the user message and Use Emojies\n\nresponse:",
25
+ "parameters": {
26
+ "top_k": 100,
27
+ "top_p": 0.9,
28
+ "temperature": 0.5,
29
+ "repetition_penalty": 1.2,
30
+ "max_new_tokens": 512,
31
+ "max_time": 0,
32
+ "return_full_text": True,
33
+ "num_return_sequences": 1,
34
+ "do_sample": False
35
+ },
36
+ "options": {
37
+ "use_cache": False,
38
+ "wait_for_model": False
39
+ }
40
+ }
41
+
42
+ output = query(new_query)
43
+
44
+ generated_text = output[0]['generated_text']
45
+
46
+ response_start = generated_text.find('response:') + len('response:')
47
+ response_end = generated_text.find('(end response)')
48
+
49
+ response_text = generated_text[response_start:response_end].strip()
50
+
51
+ note_index = response_text.find("Note:")
52
+ if note_index != -1:
53
+ response_text = response_text[:note_index].strip()
54
+
55
+ instruction_index = response_text.find("### Instruction:")
56
+ if instruction_index != -1:
57
+ response_text = response_text[:instruction_index].strip()
58
+
59
+ response_text = response_text.strip().replace('\n', '<br>')
60
+
61
+ return response_text
62
+ def get_bitcoin_price():
63
+ current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
64
+ url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
65
+ response = requests.get(url)
66
+
67
+ if response.status_code == 200:
68
+ data = response.json()
69
+ bitcoin_price = data['bpi']['USD']['rate']
70
+ return bitcoin_price, current_time
71
+
72
+
73
+
74
+ @app.route('/assets/<path:path>')
75
+ def send_static(path):
76
+ return send_from_directory('assets', path)
77
+
78
 
79
  @app.route('/')
80
+ def index():
81
+ result = get_div_content(url)
82
+ news = get_news(url)
83
+ return render_template('admin.html')
84
+
85
+ @app.route('/chat', methods=['POST'])
86
+ def chat():
87
+ user_input = request.get_json()['user_input']
88
+
89
+ # Generate AI response based on user input
90
+ response_text = generate_response(user_input)
91
+ conversation_history.append({"User": user_input, "NIRON": response_text})
92
+
93
+ # Update bitcoin price and current time
94
+ bitcoin_price, current_time = get_bitcoin_price()
95
+
96
+ return response_text
97
+
98
+ @app.route('/trans')
99
+ def trans():
100
+ news = get_news(url)
101
+ result = get_div_content(url)
102
+
103
+ return render_template('arabic.html')
104
+
105
+
106
+ @app.route('/arabic', methods=['POST'])
107
+ def arabic():
108
+ user_input = request.get_json()['user_input']
109
+
110
+ # Generate AI response based on user input
111
+ response_text = generate_response(user_input)
112
+
113
+ # Translate the response to Arabic
114
+ translator = Translator()
115
+ translated_response = translator.translate(response_text, dest='ar').text
116
+
117
+ conversation_history.append({"User": user_input, "AI": response_text})
118
+
119
+ # Update bitcoin price and current time
120
+ bitcoin_price, current_time = get_bitcoin_price()
121
+ return translated_response
122
+
123
+ def get_div_content(url):
124
+ response = requests.get(url)
125
+ soup = BeautifulSoup(response.content, 'html.parser')
126
+
127
+ div_content = soup.find('div', {'id': '45'})
128
+ if div_content:
129
+ return div_content
130
+ else:
131
+ return None
132
+
133
+ url = "https://dooratre-info.hf.space/?logs=container&__sign=eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sIm9uQmVoYWxmT2YiOnsia2luZCI6InVzZXIiLCJfaWQiOiI2NWIyYzMyNjJiZTk2NjBmMGIxMjg0MDAiLCJ1c2VyIjoiRG9vcmF0cmUifSwiaWF0IjoxNzEyNjgwNTY4LCJzdWIiOiIvc3BhY2VzL0Rvb3JhdHJlL2luZm8iLCJleHAiOjE3MTI3NjY5NjgsImlzcyI6Imh0dHBzOi8vaHVnZ2luZ2ZhY2UuY28ifQ.R_PX6Hw5SMheYTQWPGe1Qla9q8gVBU0mAFF_u8Iad06jSpZ9sPzZqquSowWn7PGVLRYBW21DnvqSwXIoNZ4CAA"
134
+
135
+ result = get_div_content(url)
136
+ print(result)
137
+
138
+
139
+
140
+ @app.route('/clear_history')
141
+ def clear_history():
142
+ global conversation_history
143
+ conversation_history = []
144
+ return 'Conversation history cleared successfully'
145
+
146
+
147
+
148
+ def get_news(url):
149
+ response = requests.get(url)
150
+ soup = BeautifulSoup(response.content, "html.parser")
151
+
152
+ news = []
153
+ for text in soup.stripped_strings:
154
+ news.append(text)
155
+
156
+ return news
157
+
158
+ url = "https://www.newsbtc.com/"
159
+ news = get_news(url)
160
+ print(news)
161
 
162
  if __name__ == '__main__':
163
  app.run(host="0.0.0.0", port=7860)