Spaces:
Running
Running
File size: 5,747 Bytes
8d14bb9 8a5db1d db6e2f8 8d14bb9 8a5db1d 8d14bb9 db6e2f8 7ecf241 8d14bb9 8a5db1d 8d14bb9 8a5db1d 8d14bb9 8a5db1d 8d14bb9 8a5db1d 8d14bb9 8a5db1d db6e2f8 7ecf241 51eabf2 |
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import requests
from flask import Flask, render_template, request, send_from_directory
from datetime import datetime
from bs4 import BeautifulSoup
app = Flask(__name__)
with open('i.txt', 'r') as file:
data = file.read()
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
headers = {"Authorization": f"Bearer hf{data}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
API_URLAR = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-tc-big-en-ar"
def queryar(ar):
response = requests.post(API_URLAR, headers=headers, json=ar)
return response.json()
conversation_history = []
def generate_response(user_input):
bitcoin_price, current_time = get_bitcoin_price()
result = get_div_content(url)
new_query = {
"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:",
"parameters": {
"top_k": 100,
"top_p": 0.9,
"temperature": 0.5,
"repetition_penalty": 1.2,
"max_new_tokens": 512,
"max_time": 0,
"return_full_text": True,
"num_return_sequences": 1,
"do_sample": False
},
"options": {
"use_cache": False,
"wait_for_model": False
}
}
output = query(new_query)
generated_text = output[0]['generated_text']
response_start = generated_text.find('response:') + len('response:')
response_end = generated_text.find('(end response)')
response_text = generated_text[response_start:response_end].strip()
note_index = response_text.find("Note:")
if note_index != -1:
response_text = response_text[:note_index].strip()
instruction_index = response_text.find("### Instruction:")
if instruction_index != -1:
response_text = response_text[:instruction_index].strip()
response_text = response_text.strip().replace('\n', '<br>')
return response_text
def get_bitcoin_price():
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
bitcoin_price = data['bpi']['USD']['rate']
return bitcoin_price, current_time
@app.route('/assets/<path:path>')
def send_static(path):
return send_from_directory('assets', path)
@app.route('/')
def index():
result = get_div_content(url)
news = get_news(url)
return render_template('admin.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.get_json()['user_input']
# Generate AI response based on user input
response_text = generate_response(user_input)
conversation_history.append({"User": user_input, "NIRON": response_text})
# Update bitcoin price and current time
bitcoin_price, current_time = get_bitcoin_price()
return response_text
@app.route('/trans')
def trans():
news = get_news(url)
result = get_div_content(url)
return render_template('arabic.html')
@app.route('/arabic', methods=['POST'])
def arabic():
user_input = request.get_json()['user_input']
# Generate AI response based on user input
response_text = generate_response(user_input)
conversation_history.append({"User": user_input, "\nAI": response_text})
# Translate the response to Arabic using the Hugging Face API
translation_payload = {
"inputs": response_text,
}
output = queryar(translation_payload)
translated_response = output[0]['translation_text']
# Append the conversation history
# Update bitcoin price and current time
bitcoin_price, current_time = get_bitcoin_price()
return translated_response
def get_div_content(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
div_content = soup.find('div', {'id': '45'})
if div_content:
return div_content
else:
return None
url = "https://dooratre-info.hf.space/?logs=container&__sign=eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sIm9uQmVoYWxmT2YiOnsia2luZCI6InVzZXIiLCJfaWQiOiI2NWIyYzMyNjJiZTk2NjBmMGIxMjg0MDAiLCJ1c2VyIjoiRG9vcmF0cmUifSwiaWF0IjoxNzEyNjgwNTY4LCJzdWIiOiIvc3BhY2VzL0Rvb3JhdHJlL2luZm8iLCJleHAiOjE3MTI3NjY5NjgsImlzcyI6Imh0dHBzOi8vaHVnZ2luZ2ZhY2UuY28ifQ.R_PX6Hw5SMheYTQWPGe1Qla9q8gVBU0mAFF_u8Iad06jSpZ9sPzZqquSowWn7PGVLRYBW21DnvqSwXIoNZ4CAA"
result = get_div_content(url)
print(result)
@app.route('/clear_history')
def clear_history():
global conversation_history
conversation_history = []
return 'Conversation history cleared successfully'
def get_news(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
news = []
for text in soup.stripped_strings:
news.append(text)
return news
url = "https://www.newsbtc.com/"
news = get_news(url)
print(news)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860)
|