Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,12 @@ import pickle
|
|
3 |
import os
|
4 |
import re
|
5 |
import numpy as np
|
|
|
6 |
import torchvision
|
7 |
import nltk
|
8 |
import torch
|
9 |
import pandas as pd
|
10 |
-
import streamlit as st
|
11 |
import requests
|
12 |
-
import threading
|
13 |
import zipfile
|
14 |
import tempfile
|
15 |
from PyPDF2 import PdfReader
|
@@ -36,13 +35,6 @@ from safetensors.torch import safe_open
|
|
36 |
nltk.download('punkt_tab')
|
37 |
|
38 |
app = FastAPI()
|
39 |
-
|
40 |
-
with open("style.css") as f:
|
41 |
-
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
42 |
-
# Streamlit interface
|
43 |
-
def start_streamlit():
|
44 |
-
st.title("My Hugging Face App")
|
45 |
-
st.text_input("Ask a question:")
|
46 |
|
47 |
app.add_middleware(
|
48 |
CORSMiddleware,
|
@@ -511,9 +503,99 @@ def translate_en_to_ar(text):
|
|
511 |
print(f"Error during English to Arabic translation: {e}")
|
512 |
return None
|
513 |
|
514 |
-
@app.get("/")
|
515 |
async def root():
|
516 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
517 |
|
518 |
@app.get("/health")
|
519 |
async def health_check():
|
@@ -662,66 +744,3 @@ if not init_success:
|
|
662 |
if __name__ == "__main__":
|
663 |
import uvicorn
|
664 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
665 |
-
|
666 |
-
# Chat API
|
667 |
-
st.header("Chat API")
|
668 |
-
chat_query = st.text_input("Enter your chat query:")
|
669 |
-
language_code = st.selectbox("Select language", ["English", "Arabic"])
|
670 |
-
|
671 |
-
if st.button("Submit Chat Query"):
|
672 |
-
response = requests.post(f"{BASE_URL}/api/chat", json={
|
673 |
-
"query": chat_query,
|
674 |
-
"language_code": 0 if language_code == "Arabic" else 1
|
675 |
-
})
|
676 |
-
if response.status_code == 200:
|
677 |
-
st.success("Response: " + response.json()['response'])
|
678 |
-
else:
|
679 |
-
st.error(f"Error: {response.status_code} - {response.text}")
|
680 |
-
|
681 |
-
# Resources API
|
682 |
-
st.header("Resources API")
|
683 |
-
conditions = st.text_input("Enter medical conditions:")
|
684 |
-
daily_symptoms = st.text_area("Enter daily symptoms:")
|
685 |
-
count = st.number_input("Number of resources", min_value=1, max_value=10, value=5)
|
686 |
-
|
687 |
-
if st.button("Submit Resources Query"):
|
688 |
-
response = requests.post(f"{BASE_URL}/api/resources", json={
|
689 |
-
"conditions": conditions,
|
690 |
-
"daily_symptoms": daily_symptoms,
|
691 |
-
"count": count
|
692 |
-
})
|
693 |
-
if response.status_code == 200:
|
694 |
-
resources = response.json()
|
695 |
-
st.write("Relevant Resources:")
|
696 |
-
for resource in resources:
|
697 |
-
st.markdown(f"- **{resource['title']}**: [Link]({resource['url']})")
|
698 |
-
else:
|
699 |
-
st.error(f"Error: {response.status_code} - {response.text}")
|
700 |
-
|
701 |
-
# Recipes API
|
702 |
-
st.header("Recipes API")
|
703 |
-
recipe_conditions = st.text_input("Enter conditions for recipes:")
|
704 |
-
recipe_symptoms = st.text_area("Enter symptoms for recipes:")
|
705 |
-
recipe_count = st.number_input("Number of recipes", min_value=1, max_value=10, value=5)
|
706 |
-
|
707 |
-
if st.button("Submit Recipes Query"):
|
708 |
-
response = requests.post(f"{BASE_URL}/api/recipes", json={
|
709 |
-
"conditions": recipe_conditions,
|
710 |
-
"daily_symptoms": recipe_symptoms,
|
711 |
-
"count": recipe_count
|
712 |
-
})
|
713 |
-
if response.status_code == 200:
|
714 |
-
recipes = response.json()['recipes']
|
715 |
-
st.write("Relevant Recipes:")
|
716 |
-
for recipe in recipes:
|
717 |
-
st.markdown(f"- **{recipe['title']}**: [Link]({recipe['url']})")
|
718 |
-
else:
|
719 |
-
st.error(f"Error: {response.status_code} - {response.text}")
|
720 |
-
if __name__ == "__main__":
|
721 |
-
# Start FastAPI in a background thread
|
722 |
-
fastapi_thread = threading.Thread(target=run_fastapi)
|
723 |
-
fastapi_thread.start()
|
724 |
-
|
725 |
-
# Start Streamlit
|
726 |
-
start_streamlit()
|
727 |
-
|
|
|
3 |
import os
|
4 |
import re
|
5 |
import numpy as np
|
6 |
+
from fastapi.responses import HTMLResponse
|
7 |
import torchvision
|
8 |
import nltk
|
9 |
import torch
|
10 |
import pandas as pd
|
|
|
11 |
import requests
|
|
|
12 |
import zipfile
|
13 |
import tempfile
|
14 |
from PyPDF2 import PdfReader
|
|
|
35 |
nltk.download('punkt_tab')
|
36 |
|
37 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
app.add_middleware(
|
40 |
CORSMiddleware,
|
|
|
503 |
print(f"Error during English to Arabic translation: {e}")
|
504 |
return None
|
505 |
|
506 |
+
@app.get("/", response_class=HTMLResponse)
|
507 |
async def root():
|
508 |
+
return """
|
509 |
+
<!DOCTYPE html>
|
510 |
+
<html>
|
511 |
+
<head>
|
512 |
+
<title>Simple User Interface</title>
|
513 |
+
<style>
|
514 |
+
body {
|
515 |
+
font-family: Arial, sans-serif;
|
516 |
+
margin: 20px;
|
517 |
+
background-color: #f8f9fa;
|
518 |
+
}
|
519 |
+
.container {
|
520 |
+
max-width: 600px;
|
521 |
+
margin: auto;
|
522 |
+
padding: 20px;
|
523 |
+
background: white;
|
524 |
+
border-radius: 8px;
|
525 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
526 |
+
}
|
527 |
+
h1 {
|
528 |
+
color: #333;
|
529 |
+
text-align: center;
|
530 |
+
}
|
531 |
+
label {
|
532 |
+
display: block;
|
533 |
+
margin-bottom: 8px;
|
534 |
+
font-weight: bold;
|
535 |
+
}
|
536 |
+
input, select, button {
|
537 |
+
width: 100%;
|
538 |
+
padding: 10px;
|
539 |
+
margin-bottom: 20px;
|
540 |
+
border: 1px solid #ccc;
|
541 |
+
border-radius: 5px;
|
542 |
+
}
|
543 |
+
button {
|
544 |
+
background-color: #007bff;
|
545 |
+
color: white;
|
546 |
+
font-weight: bold;
|
547 |
+
cursor: pointer;
|
548 |
+
}
|
549 |
+
button:hover {
|
550 |
+
background-color: #0056b3;
|
551 |
+
}
|
552 |
+
.response {
|
553 |
+
font-size: 1rem;
|
554 |
+
color: #28a745;
|
555 |
+
font-weight: bold;
|
556 |
+
margin-top: 20px;
|
557 |
+
}
|
558 |
+
</style>
|
559 |
+
</head>
|
560 |
+
<body>
|
561 |
+
<div class="container">
|
562 |
+
<h1>AI Query Interface</h1>
|
563 |
+
<form id="queryForm">
|
564 |
+
<label for="query">Enter your query:</label>
|
565 |
+
<input type="text" id="query" name="query" placeholder="Type your question..." required>
|
566 |
+
|
567 |
+
<label for="language">Choose language:</label>
|
568 |
+
<select id="language" name="language">
|
569 |
+
<option value="1">English</option>
|
570 |
+
<option value="0">Arabic</option>
|
571 |
+
</select>
|
572 |
+
|
573 |
+
<button type="button" onclick="sendQuery()">Submit</button>
|
574 |
+
</form>
|
575 |
+
<div class="response" id="responseOutput"></div>
|
576 |
+
</div>
|
577 |
+
|
578 |
+
<script>
|
579 |
+
async function sendQuery() {
|
580 |
+
const query = document.getElementById('query').value;
|
581 |
+
const language = document.getElementById('language').value;
|
582 |
+
|
583 |
+
const response = await fetch('/api/chat', {
|
584 |
+
method: 'POST',
|
585 |
+
headers: {
|
586 |
+
'Content-Type': 'application/json',
|
587 |
+
},
|
588 |
+
body: JSON.stringify({ query, language_code: parseInt(language) }),
|
589 |
+
});
|
590 |
+
|
591 |
+
const result = await response.json();
|
592 |
+
document.getElementById('responseOutput').innerText = result.response || 'No response';
|
593 |
+
}
|
594 |
+
</script>
|
595 |
+
</body>
|
596 |
+
</html>
|
597 |
+
"""
|
598 |
+
|
599 |
|
600 |
@app.get("/health")
|
601 |
async def health_check():
|
|
|
744 |
if __name__ == "__main__":
|
745 |
import uvicorn
|
746 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|