Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- .github/workflows/update_space.yml +28 -0
- .gitignore +8 -0
- Main2.py +329 -0
- README.md +2 -8
- requirements.txt +13 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.github/workflows/update_space.yml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Run Python script
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
build:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout
|
| 14 |
+
uses: actions/checkout@v2
|
| 15 |
+
|
| 16 |
+
- name: Set up Python
|
| 17 |
+
uses: actions/setup-python@v2
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.9'
|
| 20 |
+
|
| 21 |
+
- name: Install Gradio
|
| 22 |
+
run: python -m pip install gradio
|
| 23 |
+
|
| 24 |
+
- name: Log in to Hugging Face
|
| 25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
| 26 |
+
|
| 27 |
+
- name: Deploy to Spaces
|
| 28 |
+
run: gradio deploy
|
.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.env
|
| 3 |
+
Main2copy.py
|
| 4 |
+
Main2copy1.py
|
| 5 |
+
Main.py
|
| 6 |
+
.gradio/
|
| 7 |
+
.env
|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
from flask import Flask
|
| 3 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 4 |
+
from flask_migrate import Migrate
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import requests
|
| 8 |
+
from contextlib import contextmanager
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import uuid
|
| 11 |
+
import logging
|
| 12 |
+
import speech_recognition as sr
|
| 13 |
+
from moviepy import VideoFileClip
|
| 14 |
+
from sqlalchemy import inspect, text
|
| 15 |
+
|
| 16 |
+
# Configure logging
|
| 17 |
+
logging.basicConfig(level=logging.INFO,
|
| 18 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# Load environment variables from .env file
|
| 22 |
+
load_dotenv()
|
| 23 |
+
|
| 24 |
+
# Initialize Flask app and SQLAlchemy
|
| 25 |
+
app = Flask(__name__)
|
| 26 |
+
database_url = os.getenv('DATABASE_URL')
|
| 27 |
+
if not database_url:
|
| 28 |
+
raise ValueError("DATABASE_URL is not set in the .env file or environment.")
|
| 29 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = database_url
|
| 30 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 31 |
+
db = SQLAlchemy(app)
|
| 32 |
+
migrate = Migrate(app, db)
|
| 33 |
+
|
| 34 |
+
# Set Gemini API key from environment variable
|
| 35 |
+
gemini_api_key = os.getenv('GEMINI_API_KEY')
|
| 36 |
+
gemini_api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
|
| 37 |
+
|
| 38 |
+
# Database models
|
| 39 |
+
class Character(db.Model):
|
| 40 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 41 |
+
name = db.Column(db.String(100), nullable=False, unique=True)
|
| 42 |
+
description = db.Column(db.Text, nullable=False)
|
| 43 |
+
prompt_template = db.Column(db.Text, nullable=False)
|
| 44 |
+
|
| 45 |
+
class Conversation(db.Model):
|
| 46 |
+
__tablename__ = 'conversation'
|
| 47 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 48 |
+
character_id = db.Column(db.Integer, db.ForeignKey('character.id'), nullable=False)
|
| 49 |
+
user_input = db.Column(db.Text, nullable=True)
|
| 50 |
+
bot_response = db.Column(db.Text, nullable=False)
|
| 51 |
+
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
| 52 |
+
chat_id = db.Column(db.String(36), nullable=True)
|
| 53 |
+
user_id = db.Column(db.Integer, nullable=False)
|
| 54 |
+
|
| 55 |
+
@contextmanager
|
| 56 |
+
def app_context():
|
| 57 |
+
with app.app_context():
|
| 58 |
+
yield
|
| 59 |
+
|
| 60 |
+
# def reset_and_initialize_database():
|
| 61 |
+
# """Drop existing tables, recreate them, and verify the schema."""
|
| 62 |
+
# with app_context():
|
| 63 |
+
# try:
|
| 64 |
+
# db.session.execute(text("DROP TABLE IF EXISTS conversation CASCADE;"))
|
| 65 |
+
# db.session.execute(text("DROP TABLE IF EXISTS character CASCADE;"))
|
| 66 |
+
# db.session.commit()
|
| 67 |
+
# logger.info("Dropped existing tables 'conversation' and 'character'.")
|
| 68 |
+
|
| 69 |
+
# db.create_all()
|
| 70 |
+
# logger.info("Database tables recreated successfully.")
|
| 71 |
+
|
| 72 |
+
# inspector = inspect(db.engine)
|
| 73 |
+
# columns = inspector.get_columns('character')
|
| 74 |
+
# column_names = [col['name'] for col in columns]
|
| 75 |
+
# logger.info(f"Columns in 'character' table: {column_names}")
|
| 76 |
+
# if 'prompt_template' not in column_names:
|
| 77 |
+
# raise RuntimeError("Failed to create 'prompt_template' column in 'character' table.")
|
| 78 |
+
# except Exception as e:
|
| 79 |
+
# db.session.rollback()
|
| 80 |
+
# logger.error(f"Error resetting database: {e}")
|
| 81 |
+
# raise
|
| 82 |
+
|
| 83 |
+
def add_predefined_characters():
|
| 84 |
+
with app_context():
|
| 85 |
+
characters = [
|
| 86 |
+
{"name": "Chuck the Clown", "description": "A funny clown who tells jokes and entertains.", "prompt_template": "You are Chuck the Clown, always ready with a joke and entertainment. Be upbeat, silly, and include jokes in your responses."},
|
| 87 |
+
{"name": "Sarcastic Pirate", "description": "A pirate with a sharp tongue and a love for treasure.", "prompt_template": "You are a Sarcastic Pirate, ready to share your tales of adventure. Use pirate slang, be witty, sarcastic, and mention your love for treasure and the sea."},
|
| 88 |
+
{"name": "Professor Sage", "description": "A wise professor knowledgeable about many subjects.", "prompt_template": "You are Professor Sage, sharing wisdom and knowledge. Be scholarly, thoughtful, and provide educational information in your responses."}
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
for char_data in characters:
|
| 92 |
+
if not Character.query.filter_by(name=char_data["name"]).first():
|
| 93 |
+
new_character = Character(name=char_data["name"], description=char_data["description"], prompt_template=char_data["prompt_template"])
|
| 94 |
+
db.session.add(new_character)
|
| 95 |
+
logger.info(f"Adding predefined character: {char_data['name']}")
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
db.session.commit()
|
| 99 |
+
logger.info("Predefined characters added successfully.")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
db.session.rollback()
|
| 102 |
+
logger.error(f"Error adding predefined characters: {e}")
|
| 103 |
+
|
| 104 |
+
def add_character(name, description, prompt_template):
|
| 105 |
+
with app_context():
|
| 106 |
+
try:
|
| 107 |
+
if Character.query.filter_by(name=name).first():
|
| 108 |
+
return f"Character '{name}' already exists!"
|
| 109 |
+
new_character = Character(name=name, description=description, prompt_template=prompt_template)
|
| 110 |
+
db.session.add(new_character)
|
| 111 |
+
db.session.commit()
|
| 112 |
+
logger.info(f"Successfully added character: {name}")
|
| 113 |
+
return f"Character '{name}' added successfully!\nDescription: {description}"
|
| 114 |
+
except Exception as e:
|
| 115 |
+
db.session.rollback()
|
| 116 |
+
logger.error(f"Error adding character: {e}")
|
| 117 |
+
return f"An error occurred while adding the character: {str(e)}"
|
| 118 |
+
|
| 119 |
+
def get_existing_characters():
|
| 120 |
+
with app_context():
|
| 121 |
+
try:
|
| 122 |
+
characters = Character.query.all()
|
| 123 |
+
return [(char.name, char.description) for char in characters]
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logger.error(f"Error retrieving characters: {e}")
|
| 126 |
+
return [("Error retrieving characters", str(e))]
|
| 127 |
+
|
| 128 |
+
def chat_with_character(character_name, user_input, user_id, chat_id=None):
|
| 129 |
+
with app_context():
|
| 130 |
+
try:
|
| 131 |
+
if not gemini_api_key:
|
| 132 |
+
raise ValueError("GEMINI_API_KEY is not set in the environment.")
|
| 133 |
+
|
| 134 |
+
character = Character.query.filter_by(name=character_name).first()
|
| 135 |
+
if not character:
|
| 136 |
+
return "Character not found.", None
|
| 137 |
+
if not chat_id:
|
| 138 |
+
chat_id = str(uuid.uuid4())
|
| 139 |
+
previous_conversations = Conversation.query.filter_by(user_id=user_id).order_by(Conversation.timestamp).all()
|
| 140 |
+
context_prompt = " ".join([f"User: {conv.user_input}\nBot: {conv.bot_response}" for conv in previous_conversations])
|
| 141 |
+
prompt_template = character.prompt_template
|
| 142 |
+
full_prompt = f"{prompt_template}\n{context_prompt}\nUser: {user_input}\nBot:"
|
| 143 |
+
|
| 144 |
+
payload = {"contents": [{"parts": [{"text": full_prompt}]}]}
|
| 145 |
+
headers = {'Content-Type': 'application/json'}
|
| 146 |
+
response = requests.post(gemini_api_url, headers=headers, json=payload, params={'key': gemini_api_key})
|
| 147 |
+
|
| 148 |
+
if response.status_code == 200:
|
| 149 |
+
response_data = response.json()
|
| 150 |
+
if 'candidates' in response_data and response_data['candidates']:
|
| 151 |
+
bot_response = response_data['candidates'][0]['content']['parts'][0]['text']
|
| 152 |
+
conversation = Conversation(character_id=character.id, user_input=user_input, bot_response=bot_response, chat_id=chat_id, user_id=user_id)
|
| 153 |
+
db.session.add(conversation)
|
| 154 |
+
db.session.commit()
|
| 155 |
+
logger.info(f"Saved conversation with chat_id: {chat_id}")
|
| 156 |
+
return bot_response, chat_id
|
| 157 |
+
else:
|
| 158 |
+
return "An error occurred while generating content: Unexpected response format.", chat_id
|
| 159 |
+
else:
|
| 160 |
+
logger.error(f"Error from Gemini API: {response.json()}")
|
| 161 |
+
return f"An error occurred while generating content: {response.status_code} - {response.text}", chat_id
|
| 162 |
+
except Exception as e:
|
| 163 |
+
logger.error(f"Unexpected error in chat_with_character: {e}")
|
| 164 |
+
return f"An unexpected error occurred: {str(e)}", chat_id
|
| 165 |
+
|
| 166 |
+
def speech_to_text(audio_file):
|
| 167 |
+
recognizer = sr.Recognizer()
|
| 168 |
+
with sr.AudioFile(audio_file) as source:
|
| 169 |
+
audio_data = recognizer.record(source)
|
| 170 |
+
try:
|
| 171 |
+
return recognizer.recognize_google(audio_data)
|
| 172 |
+
except sr.UnknownValueError:
|
| 173 |
+
logger.error("Could not understand audio")
|
| 174 |
+
return None
|
| 175 |
+
except sr.RequestError as e:
|
| 176 |
+
logger.error(f"Could not request results from Google Speech Recognition service; {e}")
|
| 177 |
+
return None
|
| 178 |
+
|
| 179 |
+
def extract_audio_from_video(video_file):
|
| 180 |
+
audio_file_path = "temp_audio.wav"
|
| 181 |
+
try:
|
| 182 |
+
with VideoFileClip(video_file) as video:
|
| 183 |
+
video.audio.write_audiofile(audio_file_path)
|
| 184 |
+
except Exception as e:
|
| 185 |
+
logger.error(f"Error extracting audio from video: {e}")
|
| 186 |
+
return None
|
| 187 |
+
return audio_file_path
|
| 188 |
+
|
| 189 |
+
def get_chat_history(user_id):
|
| 190 |
+
with app_context():
|
| 191 |
+
try:
|
| 192 |
+
conversations = Conversation.query.filter_by(user_id=user_id).order_by(Conversation.timestamp).all()
|
| 193 |
+
return conversations
|
| 194 |
+
except Exception as e:
|
| 195 |
+
logger.error(f"Error retrieving chat history: {e}")
|
| 196 |
+
return []
|
| 197 |
+
|
| 198 |
+
def create_interface():
|
| 199 |
+
with app_context():
|
| 200 |
+
add_predefined_characters()
|
| 201 |
+
|
| 202 |
+
with gr.Blocks(title="Character Chat System", theme=gr.themes.Default()) as iface:
|
| 203 |
+
current_chat_id = gr.State(value=None)
|
| 204 |
+
user_id = gr.State(value=None)
|
| 205 |
+
chat_messages = gr.State(value=[])
|
| 206 |
+
|
| 207 |
+
gr.Markdown("# π Character Chat System π", elem_id="title")
|
| 208 |
+
|
| 209 |
+
with gr.Tab("Sign In"):
|
| 210 |
+
user_id_input = gr.Textbox(label="User ID (Numeric)", placeholder="Enter your numeric User ID (e.g., 123)", elem_id="user_id_input", interactive=True, lines=2)
|
| 211 |
+
sign_in_btn = gr.Button("Sign In", variant="primary")
|
| 212 |
+
sign_in_response = gr.Textbox(label="Sign In Response", interactive=False)
|
| 213 |
+
|
| 214 |
+
def sign_in(user_id_input):
|
| 215 |
+
try:
|
| 216 |
+
user_id_int = int(user_id_input) # Convert to integer
|
| 217 |
+
return f"Welcome, User {user_id_int}!", user_id_int
|
| 218 |
+
except ValueError:
|
| 219 |
+
return "Please enter a valid numeric User ID (e.g., 123)!", None
|
| 220 |
+
|
| 221 |
+
sign_in_btn.click(fn=sign_in, inputs=[user_id_input], outputs=[sign_in_response, user_id])
|
| 222 |
+
|
| 223 |
+
with gr.Tab("Admin: Add Character"):
|
| 224 |
+
with gr.Row():
|
| 225 |
+
with gr.Column():
|
| 226 |
+
name_input = gr.Textbox(label="Character Name", placeholder="Enter character name", elem_id="name_input")
|
| 227 |
+
description_input = gr.Textbox(label="Character Description", placeholder="Enter character description", elem_id="description_input")
|
| 228 |
+
prompt_input = gr.Textbox(label="Prompt Template", placeholder="Enter character prompt template", elem_id="prompt_input", lines=3)
|
| 229 |
+
add_character_btn = gr.Button("Add Character", elem_id="add_character_btn", variant="primary")
|
| 230 |
+
add_character_response = gr.Textbox(label="Response", interactive=False, elem_id="response_output")
|
| 231 |
+
add_character_btn.click(fn=add_character, inputs=[name_input, description_input, prompt_input], outputs=[add_character_response])
|
| 232 |
+
with gr.Column():
|
| 233 |
+
gr.Markdown("## π Existing Characters π", elem_id="existing_chars_title")
|
| 234 |
+
existing_characters = get_existing_characters()
|
| 235 |
+
character_list = gr.Dataframe(value=existing_characters, headers=["Name", "Description"], interactive=False, elem_id="character_list")
|
| 236 |
+
refresh_characters_btn = gr.Button("Refresh Character List")
|
| 237 |
+
refresh_characters_btn.click(fn=lambda: gr.update(value=get_existing_characters()), outputs=[character_list])
|
| 238 |
+
|
| 239 |
+
with gr.Tab("Chat with Character"):
|
| 240 |
+
with gr.Row():
|
| 241 |
+
character_dropdown = gr.Dropdown(label="Choose Character", choices=[char[0] for char in get_existing_characters()], elem_id="character_dropdown")
|
| 242 |
+
chat_id_display = gr.Textbox(label="Current Chat ID", interactive=False, elem_id="chat_id_display")
|
| 243 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message or use audio/video input", elem_id="user_input", lines=2)
|
| 244 |
+
audio_input = gr.Audio(label="Audio Input", type="filepath", elem_id="audio_input")
|
| 245 |
+
video_input = gr.Video(label="Video Input", elem_id="video_input")
|
| 246 |
+
chat_btn = gr.Button("Send", variant="primary")
|
| 247 |
+
chat_response = gr.Chatbot(label="Chat Responses", elem_id="chat_response", height=300, type="messages") # Updated to 'messages' type
|
| 248 |
+
|
| 249 |
+
def handle_chat(character_name, user_input, audio_file, video_file, user_id, chat_messages, current_chat_id):
|
| 250 |
+
if not user_id:
|
| 251 |
+
return chat_messages, current_chat_id, "Please sign in with a numeric User ID first!"
|
| 252 |
+
if not character_name:
|
| 253 |
+
return chat_messages, current_chat_id, "Please select a character!"
|
| 254 |
+
final_input = user_input or ""
|
| 255 |
+
|
| 256 |
+
if audio_file:
|
| 257 |
+
audio_text = speech_to_text(audio_file)
|
| 258 |
+
if audio_text:
|
| 259 |
+
final_input += f" {audio_text}"
|
| 260 |
+
else:
|
| 261 |
+
chat_messages.append({"role": "assistant", "content": "Could not understand audio."})
|
| 262 |
+
return chat_messages, current_chat_id, None
|
| 263 |
+
|
| 264 |
+
if video_file:
|
| 265 |
+
audio_file_path = extract_audio_from_video(video_file)
|
| 266 |
+
if audio_file_path:
|
| 267 |
+
video_text = speech_to_text(audio_file_path)
|
| 268 |
+
if video_text:
|
| 269 |
+
final_input += f" {video_text}"
|
| 270 |
+
chat_messages.append({"role": "user", "content": "Video uploaded"})
|
| 271 |
+
else:
|
| 272 |
+
chat_messages.append({"role": "assistant", "content": "Failed to extract audio from video."})
|
| 273 |
+
return chat_messages, current_chat_id, None
|
| 274 |
+
|
| 275 |
+
if not final_input.strip():
|
| 276 |
+
return chat_messages, current_chat_id, "Please provide a message, audio, or video!"
|
| 277 |
+
|
| 278 |
+
response, new_chat_id = chat_with_character(character_name, final_input, user_id, current_chat_id)
|
| 279 |
+
chat_messages.append({"role": "user", "content": final_input})
|
| 280 |
+
chat_messages.append({"role": "assistant", "content": response})
|
| 281 |
+
return chat_messages, new_chat_id, new_chat_id
|
| 282 |
+
|
| 283 |
+
chat_btn.click(fn=handle_chat, inputs=[character_dropdown, user_input, audio_input, video_input, user_id, chat_messages, current_chat_id], outputs=[chat_response, current_chat_id, chat_id_display])
|
| 284 |
+
|
| 285 |
+
with gr.Tab("Chat History"):
|
| 286 |
+
with gr.Row():
|
| 287 |
+
gr.Markdown("## π View Chat History π")
|
| 288 |
+
view_history_btn = gr.Button("View History", variant="primary")
|
| 289 |
+
chat_history_display = gr.Dataframe(label="Chat History", interactive=False)
|
| 290 |
+
|
| 291 |
+
def load_chat_history(user_id):
|
| 292 |
+
if not user_id:
|
| 293 |
+
return [("Error", "Please sign in with a numeric User ID to view chat history.")]
|
| 294 |
+
history = get_chat_history(user_id)
|
| 295 |
+
return [(conv.id, f"User: {conv.user_input}\nBot: {conv.bot_response} at {conv.timestamp}") for conv in history]
|
| 296 |
+
|
| 297 |
+
view_history_btn.click(fn=load_chat_history, inputs=[user_id], outputs=[chat_history_display])
|
| 298 |
+
|
| 299 |
+
with gr.Tab("API Status"):
|
| 300 |
+
with gr.Row():
|
| 301 |
+
gr.Markdown("## π API Connection Status π")
|
| 302 |
+
check_api_btn = gr.Button("Check API Status", variant="primary")
|
| 303 |
+
api_status_display = gr.Textbox(label="API Status", interactive=False)
|
| 304 |
+
check_api_btn.click(fn=lambda: "β
API connection successful!" if requests.post(gemini_api_url, headers={'Content-Type': 'application/json'}, json={"contents": [{"parts": [{"text": "Hello"}]}]}, params={'key': gemini_api_key}).status_code == 200 else "β API connection failed!", outputs=[api_status_display])
|
| 305 |
+
|
| 306 |
+
return iface
|
| 307 |
+
|
| 308 |
+
if __name__ == "__main__":
|
| 309 |
+
if not os.getenv('DATABASE_URL'):
|
| 310 |
+
logger.error("DATABASE_URL is not set in the .env file or environment.")
|
| 311 |
+
raise ValueError("DATABASE_URL is required.")
|
| 312 |
+
if not os.getenv('GEMINI_API_KEY'):
|
| 313 |
+
logger.error("GEMINI_API_KEY is not set in the .env file or environment.")
|
| 314 |
+
raise ValueError("GEMINI_API_KEY is required.")
|
| 315 |
+
|
| 316 |
+
# with app_context():
|
| 317 |
+
# try:
|
| 318 |
+
# # reset_and_initialize_database()
|
| 319 |
+
# # add_predefined_characters()
|
| 320 |
+
# except Exception as e:
|
| 321 |
+
# logger.error(f"Error initializing database: {e}")
|
| 322 |
+
# logger.info("If the error persists, manually drop the tables using psql:")
|
| 323 |
+
# logger.info("psql \"postgresql://avnadmin:AVNS_WIH89YjY1kOIOBH-cFF@pg-197dad92-elyxir4lyf-aa02.d.aivencloud.com:20563/defaultdb?sslmode=require\"")
|
| 324 |
+
# logger.info("Then run: DROP TABLE character; DROP TABLE conversation;")
|
| 325 |
+
# raise
|
| 326 |
+
|
| 327 |
+
chat_interface = create_interface()
|
| 328 |
+
logger.info("Starting Gradio interface...")
|
| 329 |
+
chat_interface.launch(share=True)
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.19.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: chatbot
|
| 3 |
+
app_file: Main2.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.19.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
Flask-SQLAlchemy
|
| 3 |
+
python-dotenv
|
| 4 |
+
gradio
|
| 5 |
+
requests
|
| 6 |
+
speechrecognition
|
| 7 |
+
moviepy
|
| 8 |
+
gtts
|
| 9 |
+
flask_bcrypt
|
| 10 |
+
psycopg2-binary
|
| 11 |
+
jwt
|
| 12 |
+
flask-cors
|
| 13 |
+
flask_migrate
|