Spaces:
Running
Running
File size: 2,875 Bytes
42b81a8 42990eb 42b81a8 79340f2 42b81a8 79340f2 42b81a8 79340f2 42b81a8 |
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 |
import gradio as gr
import json
import os
import shutil
# import magic
# import ollama
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from langchain_community.vectorstores.qdrant import Qdrant
from langchain_core.runnables import RunnablePassthrough
from langchain_core.documents import Document
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# from langchain_community.chains import
from langchain_community.chat_models import ChatOllama
from langchain_chroma import Chroma
from hugchat import hugchat
from hugchat.login import Login
import dotenv
from utils import HuggingChat
from langchain import PromptTemplate
dotenv.load_dotenv()
class GradioApp:
def __init__(self):
# self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=32)
template = """
You are a helpful health assistant. These Human will ask you a questions about their pregnancy health.
Use following piece of context to answer the question.
If you don't know the answer, just say you don't know.
Keep the answer within 2 sentences and concise.
Context: {context}
Question: {question}
Answer:
"""
self.llm = HuggingChat(email = os.getenv("HF_EMAIL") , psw = os.getenv("HF_PASS") )
self.chain = (self.llm | StrOutputParser())
def user(self,user_message, history):
return "", history + [[user_message, None]]
def bot(self,history):
print(history)
prompt = history[-1][0] or ""
for chunks in self.chain.stream(prompt):
history[-1][1] = history[-1][1] or ""
history[-1][1] += chunks
yield history
history[-1][1] = history[-1][1] or ""
history[-1][1] += self.chain.invoke(prompt)
print(history[-1][1])
print(history)
return history
with gr.Blocks() as demo:
gradio_app = GradioApp()
# files = gr.Files(label="Upload Documents and Medical Reports", type="filepath", file_types=["pdf", "docx", "jpg", "jpeg", "png"])
# upload_button = gr.UploadButton(label="Upload Documents and Medical Reports", type="filepath", file_count='multiple', file_types=["pdf", "docx", "jpg", "jpeg", "png"], )
output_text = gr.Markdown(label="Output", value=" ")
infer_status = gr.Label("Infer Status: ", visible=False)
# upload_button.upload(gradio_app.upload_files, upload_button, [files, output_text])
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
msg.submit(gradio_app.user, [msg, chatbot], [msg, chatbot], queue=False).then(
gradio_app.bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
# demo.queue()
demo.launch(share=True)
|