File size: 4,520 Bytes
cdeb7b2
1d6a862
 
cdeb7b2
1d6a862
 
cdeb7b2
1d6a862
 
 
 
cdeb7b2
1d6a862
cdeb7b2
 
 
 
1d6a862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdeb7b2
 
 
 
 
 
 
 
1d6a862
 
 
 
 
 
 
cdeb7b2
 
1d6a862
 
 
 
 
 
 
cdeb7b2
 
 
1d6a862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_client import Client, handle_file
import os

# Define your Hugging Face token (make sure to set it as an environment variable)
HF_TOKEN = os.getenv("HF_TOKEN")  # Replace with your actual token if not using env variable

# Initialize the Gradio Client for the specified API
client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)

# Authentication function
def login(username, password):
    if username == "your_username" and password == "your_password":  # Update with actual credentials
        return True
    else:
        return False

# Function to handle different API calls based on user input
def handle_api_call(username, password, audio_file=None, pdf_file=None, message=None, query=None, question=None):
    if not login(username, password):
        return "Invalid credentials! Please try again."

    if audio_file:
        # Handle audio file using the appropriate API
        result = client.predict(audio=handle_file(audio_file), api_name="/process_audio")  # Example endpoint for audio processing
        return result
    elif pdf_file:
        # Handle PDF file
        pdf_result = client.predict(pdf_file=handle_file(pdf_file), client_name="rosariarossi", api_name="/process_pdf2")
        return pdf_result[1]  # Returning the string result from the PDF processing
    elif message:
        # Handle chat message
        chat_result = client.predict(
            message=message,
            client_name="rosariarossi",
            system_prompt="You are an expert assistant",
            num_retrieved_docs=10,
            num_docs_final=9,
            temperature=0,
            max_new_tokens=1024,
            top_p=1,
            top_k=20,
            penalty=1.2,
            api_name="/chat"
        )
        return chat_result
    elif query:
        # Handle search query
        search_result = client.predict(query=query, api_name="/search_with_confidence")
        return search_result
    elif question:
        # Handle question for RAG
        rag_result = client.predict(question=question, api_name="/answer_with_rag")
        return rag_result
    else:
        return "No valid input provided!"

# Create the Gradio Blocks interface
with gr.Blocks() as app:
    gr.Markdown("### Login")
    
    with gr.Row():
        username_input = gr.Textbox(label="Username", placeholder="Enter your username")
        password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
    
    audio_input = gr.Audio(label="Upload Audio File", type="filepath")
    pdf_input = gr.File(label="Upload PDF File")
    
    message_input = gr.Textbox(label="Enter Message for Chat")
    query_input = gr.Textbox(label="Enter Search Query")
    question_input = gr.Textbox(label="Enter Question for RAG")

    output_text = gr.Textbox(label="Output", interactive=False)

    # Bind the button click to the handle_api_call function
    api_button = gr.Button("Submit")
    api_button.click(
        handle_api_call,
        inputs=[username_input, password_input, audio_input, pdf_input, message_input, query_input, question_input],
        outputs=output_text
    )

# Launch the app
app.launch()





# import gradio as gr

# # Define a function for the main application
# def greet(name):
#     return f"Hello {name}!"

# # Define a function for the authentication
# def login(username, password):
#     if username == "your_username" and password == "your_password":
#         return True
#     else:
#         return False

# # Create the Gradio Blocks interface
# with gr.Blocks() as app:
#     gr.Markdown("### Login")
    
#     with gr.Row():
#         username_input = gr.Textbox(label="Username", placeholder="Enter your username")
#         password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
    
#     login_button = gr.Button("Login")
#     output_text = gr.Textbox(label="Output", interactive=False)

#     # Function to handle login and display greeting
#     def handle_login(username, password):
#         if login(username, password):
#             # Clear the password field and display the greeting
#             #password_input.clear()
#             return greet(username)
#         else:
#             return "Invalid credentials! Please try again."
    
#     # Bind the button click to the handle_login function
#     login_button.click(handle_login, inputs=[username_input, password_input], outputs=output_text)

# # Launch the app
# app.launch()