File size: 2,618 Bytes
f10850c
 
 
 
d78eadb
e4a8670
d78eadb
 
af49805
44083db
f10850c
 
 
 
1c6b41f
f10850c
1c6b41f
 
 
f10850c
 
 
e4a8670
f10850c
 
1c6b41f
 
f10850c
 
 
 
 
44083db
af49805
 
 
 
 
 
44083db
 
 
 
af49805
44083db
 
f10850c
 
 
1c6b41f
f10850c
 
 
 
 
 
 
1c6b41f
f10850c
 
 
 
 
 
 
 
 
 
 
44083db
 
af49805
44083db
 
 
af49805
44083db
af49805
44083db
 
 
 
 
 
f10850c
 
 
d78eadb
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
import os
import logging
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

from langchain_ollama import OllamaLLM
from langchain_core.callbacks.manager import CallbackManager
from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from jira_mcp_connector import JiraMCPConnector
from telegramchat import TelegramChatService

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(title="Qwen2.5 7B on HF Space")

# ================== CHANGE HERE ==================
MODEL_NAME = "qwen2.5:7b-instruct"   # Best chat version
# ================================================

def get_llm():
    callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
    return OllamaLLM(
        model=MODEL_NAME,
        temperature=0.7,
        num_ctx=8192,        # Qwen supports bigger context
        callback_manager=callback_manager,
    )

class Question(BaseModel):
    text: str


jira_connector = JiraMCPConnector(
    llm_factory=get_llm,
    logger=logger,
)


telegram_chat_service = TelegramChatService(
    llm_factory=get_llm,
    token=os.getenv("TELEGRAM_TOKEN", ""),
    logger=logger,
    jira_connector=jira_connector,
)

@app.get("/")
def read_root():
    return {
        "message": "✅ Qwen2.5 7B is running!",
        "model": MODEL_NAME,
        "endpoints": ["/ask", "/stream"]
    }

@app.post("/ask")
async def ask(question: Question):
    try:
        logger.info(f"Question: {question.text[:100]}...")
        llm = get_llm()
        response = llm.invoke(question.text)
        return {
            "question": question.text,
            "answer": response.strip()
        }
    except Exception as e:
        logger.error(f"Error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/stream")
async def stream():
    try:
        jira_connector.ensure_services_started()
        started = telegram_chat_service.start()
        return {
            "telegram_running": telegram_chat_service.is_running,
            "jira_api_running": True,
            "status": "started" if started else "already_running",
            "message": "Telegram + Jira MCP flow is active. Open Telegram and message your bot.",
        }
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"Error starting Telegram chat: {e}")
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))