Spaces:
Sleeping
Sleeping
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 | |
def send_static(path): | |
return send_from_directory('assets', path) | |
def index(): | |
result = get_div_content(url) | |
news = get_news(url) | |
return render_template('admin.html') | |
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 | |
def trans(): | |
news = get_news(url) | |
result = get_div_content(url) | |
return render_template('arabic.html') | |
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) | |
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) | |